0

Input:

{
"data": {
    "assets": [{
            "organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "sourcing",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api"
        },
        {
            "organizationId": "SASAAs",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "supply-chain",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api"
        }   
    ]
}

}

Expected output:

{
"data": {
    "assets": [{
            "organizationId": "1056bda9-2598-4fdf-bd99-db3924464a75",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "sourcing",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api",
            "domain": "sourcing"
        },
        {
            "organizationId": "SASAAs",
            "createdAt": "2018-03-14T14:41:41.154Z",
            "tags": [{
                    "value": "raml",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "rest",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "api",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "v1",
                    "key": "product-api-version",
                    "mutable": false
                },
                {
                    "value": "has-mule4-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "has-mule3-connector",
                    "key": null,
                    "mutable": false
                },
                {
                    "value": "system",
                    "key": null,
                    "mutable": true
                },
                {
                    "value": "supply-chain",
                    "key": null,
                    "mutable": true
                }
            ],
            "type": "rest-api",
            "domain": "supply-chain"
        }   
    ]
}

}

SO far, I tried this which worked partially for me.

.data.assets[] | select (.tags[].value=="sourcing") | . += {"domain":"sourcing"}

The problem is that I want this condition to apply for every object inside the array but I'm not able to do that. It is getting applied to the first object only.

Where am i doing wrong? Any suggestions please?

Naveen K Reddy
  • 429
  • 5
  • 13

1 Answers1

1

The following seems to meet the descriptive requirements:

.data.assets |=
  map( if any(.tags[].value; . == "sourcing")
       then . + {"domain":"sourcing"}
       else .
       end )

This produces the desired output except for the key-value pair "domain": "supply-chain" that is inconsistent with the descriptive requirements.

The following, by contrast, takes its cue from (that is, produces) the given output:

.data.assets |=
  map( if any(.tags[].value; . == "sourcing") then . + {"domain":"sourcing"}
       elif any(.tags[].value; . == "supply-chain") then . + {"domain":"supply-chain"}
       else . end )

Setting "domain" to all the tag values

.data.assets |= map( .domain += [.tags[].value] )
peak
  • 105,803
  • 17
  • 152
  • 177
  • This is exactly I'm looking for! 100% served my purpose. Thank you so much. But I'm no able to run this command from inside bash script? Anything wrong? `echo $response | jq .data.assets |= map( if any(.tags[].value; . == "sourcing") then . + {"domain":"sourcing"} else . end )` – Naveen K Reddy Mar 24 '18 at 09:44
  • You would have to add suitable quotation marks (e.g. echo “$response” | jq ‘...’), or use the -f command-line option. See the jq manual for details. – peak Mar 24 '18 at 12:10
  • It was my mistake earlier. I corrected the quotes and it working like charm. Upvote! I have around 6 `elif` conditions. To avoid this, is it feasible to declare array as in `arr["marketing","supply-chain","sourcing"]` and compare and achieve the same output? – Naveen K Reddy Mar 24 '18 at 13:13
  • Yes, it’s possible, and easily done once you master jq’s basics. Meanwhile, you indicated an intention to upvote .... – peak Mar 24 '18 at 17:09
  • I did upvote. They said it can’t be visible right now! – Naveen K Reddy Mar 25 '18 at 13:45
  • Will you be able to give an example on array? – Naveen K Reddy Mar 25 '18 at 14:05
  • Please look https://stackoverflow.com/questions/49553691/jq-add-capturing-group-result-outside – Naveen K Reddy Mar 29 '18 at 10:35