Reputation: 7526
In my sample json below, I can filter records where charge is null with jq -M ' map(select(.charge == null)) '
[
{
"id": 1,
"name": "vehicleA",
"state": "available",
"charge": 100
},
{
"id": 2,
"name": "vehicleB",
"state": "available",
},
{
"id": 3,
"name": "vehicleB",
"state": "available",
"charge": 50
}
]
which returns:
{
"id": 2,
"name": "vehicleB",
"state": "available",
}
How would one only get id
and the value associated with id
of the filtered records so that adding this step to the above query would return 2
?
Upvotes: 6
Views: 17999
Reputation: 14625
If you would like an array of ids for items without a charge you could use this filter:
.items | map(select(.charge == null) | .id)
If you want the values enumerated instead of being collected into an array this is better:
.items[] | select(.charge == null) | .id
Upvotes: 6
Reputation: 124648
Your question is not exactly accurate:
map(select(.charge == null))
is not an object as in the example, but an array of a single objectIn any case, you can extract the .id
from the result of map
like this:
jq -M 'map(select(.charge == null)) | .[].id' file.json
Upvotes: 5