Query using the jq

Below is the JSON data format, in which I have to do this 2 query:

  1. get the friends of all female members with id: 0.
  2. filter all the user names by the field name who are older than 30 years of age.

json data:

[
{
“_id”: “5de02668a497636c5b6a41d0”,
“index”: 0,
“guid”: “65878386-fc2a-456c-97fc-609090d5e4b4”,
“isActive”: false,
“balance”: “$3,711.31”,
“picture”: “xyz”,
“age”: 20,
“eyeColor”: “brown”,
“name”: “Flora Lang”,
“gender”: “female”,
“company”: “XERONK”,
“email”: “[email protected]”,
“phone”: “+1 (952) 499-3944”,
“address”: “285 Wyckoff Avenue, Castleton, Connecticut, 2770”,
“about”: “Dolore sint aliqua est eu adipisicing laboris exercitation tempor. Aliqua esse non veniam non amet eiusmod aliquip qui irure cupidatat aliquip aliqua mollit. Anim pariatur cupidatat tempor laboris pariatur do elit magna. Enim nulla mollit esse mollit eiusmod aliquip nostrud.\r\n”,
“registered”: “2014-10-04T01:45:52 +04:00”,
“latitude”: -48.306077,
“longitude”: -98.76451,
“tags”: [
“ipsum”,
“laborum”,
“labore”,
“id”,
“nostrud”,
“officia”,
“laborum”
],
“friends”: [
{
“id”: 0,
“name”: “Hanson Leon”
},
{
“id”: 1,
“name”: “Taylor Graham”
},
{
“id”: 2,
“name”: “Fannie Wynn”
}
],
“greeting”: “Hello, Flora Lang! You have 6 unread messages.”,
“favoriteFruit”: “strawberry”
},
{
“_id”: “5de026698f7041639b4fa9da”,
“index”: 1,
“guid”: “b53dd8ae-843e-46f4-8260-02e6227dc09d”,
“isActive”: true,
“balance”: “$2,385.68”,
“picture”: “xyz”,
“age”: 39,
“eyeColor”: “blue”,
“name”: “Bobbi Oneal”,
“gender”: “female”,
“company”: “ZAGGLE”,
“email”: “[email protected]”,
“phone”: “+1 (934) 456-2365”,
“address”: “588 Pine Street, Faywood, West Virginia, 1392”,
“about”: “Consectetur sint minim enim tempor nulla do labore elit sit ut do. Do nisi esse incididunt cillum aute et voluptate sit adipisicing sunt est. Consectetur quis ipsum laborum pariatur sint excepteur ipsum dolor minim eu. Nulla id sunt magna nostrud laboris nisi. Lorem fugiat sit mollit reprehenderit. Ex tempor qui fugiat tempor id occaecat laborum quis.\r\n”,
“registered”: “2014-09-29T06:55:59 +04:00”,
“latitude”: 68.638439,
“longitude”: -140.730665,
“tags”: [
“proident”,
“veniam”,
“pariatur”,
“deserunt”,
“elit”,
“est”,
“occaecat”
],
“friends”: [
{
“id”: 0,
“name”: “Allison Silva”
},
{
“id”: 1,
“name”: “Reeves Tyson”
},
{
“id”: 2,
“name”: “Rosanne Simpson”
}
],
“greeting”: “Hello, Bobbi Oneal! You have 9 unread messages.”,
“favoriteFruit”: “apple”
}]

The way to do this is to chain things down using a couple of tools jq gives you. After fixing your json (it’s a good idea to use the </> button to paste your code, since it gets garbled otherwise), I use the jq docs to get a few things:

  • You can convert an array to individual objects in the array by doing .[]
  • You can select an item from that group by chaining with `| select(.gender==“female”)"

So the first one is

jq '.[] | select(.gender=="female").friends | .[] |  select(.id==0)' people.json

and the second

jq '.[] | select(.age>30).name' people.json