Reputation: 81
Need to parse following for key, which have zipcodes == 73707802. For example, need to search for state key and name where one of the zipcodes is 73707802.So if I search for 73707802, it should return all names and keys (key name zipcode)
12 abc 73707802
32 cde 73707802
99 xyz 73707802
Thanks in advance..
#{
"states": [
{
"key": "12",
"name": "abc",
"city": {
"zipcodes": [
73707802,
71444504,
72646331,
73707802
]
}
},
{
"key": "32",
"name": "cde",
"city": {
"zipcodes": [
71444504,
72646331,
73707802
]
}
},
{
"key": "99",
"name": "xyz",
"city": {
"zipcodes": [
72754781,
71444504,
72646331,
73707802
]
}
}
]
}
#
Upvotes: 2
Views: 5287
Reputation: 14625
Here is a solution that uses the -r
and --arg
options with select and index.
.states[]
| select(.city.zipcodes | index($zip|tonumber))
| "\(.key) \(.name) \($zip)"
Note that in your example you only show the zip you searched for but if you want other zipcodes to be shown as well you can change the final format string.
If you put the filter above in filter.jq
and your data is in data.json
you can run it like this:
$ jq -r --arg zip 73707802 -f filter.jq data.json
12 abc 73707802
32 cde 73707802
99 xyz 73707802
Upvotes: 2