3

I want to print ID of parent element when child element value is client_release from JSON data.

if data.properties.value== "client_release"

then output should be

abcd1g2f,hirk5d7b3l

I tried below, but no luck

jq '.data[].properties[]|select(.value=="client_release")|.id'

JSON data is below:

{  
   "data":[  
      {  
         "id":"abcd1g2f",
         "resourceURI":"https://somerepo.com/service/local/privileges/abcd1g2f",
         "name":"release1",
         "description":"release1",
         "type":"target",
         "userManaged":true,
         "properties":[  
            {  
               "key":"repositoryGroupId",
               "value":""
            },
            {  
               "key":"method",
               "value":"create,read"
            },
            {  
               "key":"repositoryId",
               "value":"client_release"
            },
            {  
               "key":"repositoryTargetId",
               "value":"1"
            }
         ]
      },
      {  
         "id":"asdf1k4g",
         "resourceURI":"https://somerepo.com/service/local/privileges/asdf1k4g",
         "name":"release2",
         "description":"release2",
         "type":"target",
         "userManaged":true,
         "properties":[  
            {  
               "key":"repositoryGroupId",
               "value":""
            },
            {  
               "key":"method",
               "value":"read"
            },
            {  
               "key":"repositoryId",
               "value":"formal_release"
            },
            {  
               "key":"repositoryTargetId",
               "value":"1"
            }
         ]
      },
      {  
         "id":"hirk5d7b3l",
         "resourceURI":"https://somerepo.com/service/local/privileges/hirk5d7b3l",
         "name":"release3",
         "description":"release3",
         "type":"target",
         "userManaged":true,
         "properties":[  
            {  
               "key":"repositoryGroupId",
               "value":""
            },
            {  
               "key":"method",
               "value":"create,read"
            },
            {  
               "key":"repositoryId",
               "value":"client_release"
            },
            {  
               "key":"repositoryTargetId",
               "value":"1"
            }
         ]
      }
   ]
}

2 Answers 2

4

The idea is right, but the data[] array should be outside the select statement,

jq '.data[] | select(.properties[].value == "client_release") | .id'

To put it in the CSV format as indicated in the question, put the result into an array and use the @csv construct

jq --raw-output '[.data[] | select(.properties[].value == "client_release") | .id] | @csv'
0
2

The following filter avoids duplications and might be more efficient than using select(.properties[].value ...):

.data
| map(select(.properties | any(.[]; .value == "client_release")) | .id)
| join(",")

(You could alternatively use @csv at the end if you want the values of .id as JSON strings.)

"repositoryId"

If attention should only be paid to the value corresponding to "repositoryId", then you could use from_entries, e.g.:

.data
| map(select(.properties | from_entries.repositoryId == "client_release") | .id)
| join(",")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.