Pipe-ing
jq works on set of filters. Each filtering step produce a result which can be den further filtered
json='{"person": {"name": "Ivo", "phone": "123"}}'
echo $json | jq .person.name
"Ivo"
is the same as
echo $json | jq ".person | .name"
"Ivo"
because the first | will produce {name: ‘Ivo’} which will be then filtered.
Extract multiple fields
This is done by enumerating the fields with ,
Just do
echo $json | jq ".person | (.name,.phone)"
"Ivo"
"123"
But if you want to concatente them add echo $json | jq “.person | {name_with_phone:(.name + “-” + .phone)}” { “name_with_phone”: “Ivo-123” }
..and finally extract only the name_with_phone
echo $json | jq ".person | {name_with_phone:(.name + \"-\" + .phone)} | .name_with_phone"
"Ivo-123"
..and to get only the value add -r echo $json | jq -r “.person | {name_with_phone:(.name + “-” + .phone)} | .name_with_phone” Ivo-123
Here is an example where I grab all the location ids from all parents
cat * | jq '.deviceLocationUpdate.location | .locationId,.parent.locationId,.parent.parent.locationId,.parent.parent.parent.locationId,.parent.parent.parent.parent.locationId' | sort | uniq