[
{
"username": "new",
"attributes": [
{
"name": "attr1",
"value": "value1"
},
{
"name": "attr2",
"value": "value2"
}
]
},
{
"username": "new2",
"attributes": [
{
"name": "attr2",
"value": "value3"
},
{
"name": "attr1",
"value": "value4"
}
]
}
]
I have a json as above. From this json, I would like to get the following:
[
{
"username": "new",
"value": "value1"
},
{
"username": "new2",
"value": "value4"
}
]
The value I want to have in this object is the value for attribute named attr1. How I can achieve this with jq?
Edit: Fixed errors in the qn. I have tried
echo $USERS | jq ['.[] | {username:username, value: jq '.attributes[] | select(.name == "attr1") | .value'}'];
where $USERS contains the json data
value4bevalue3?attr1is always present you can usemap({ username, value: .attributes | map(select(.name == "attr1"))[0].value })