The -r option should not be used if the output will be fed into another JSON parser; only the last jq in the pipeline should use it – otherwise you risk feeding non-JSON data into the second jq instance.
Your last example with .[0].t seems correct and works with jq 1.6.
$ cat json | jq '.tx' | jq -r '.[0].t'
t for a
But this can be simplified to a single jq invocation, first incrementally to:
$ cat json | jq -r '.tx | .[0].t'
t for a
and then finally to:
$ cat json | jq -r '.tx[0].t'
t for a
Answer from grawity on Stack ExchangeUsing JQ to return the index number where an element in an array has a specific value.
shell - Get value of JSON [] array in jq - Unix & Linux Stack Exchange
jq: Extract element from object or array of objects
iteration - Output specific key value in object for each element in array with jq for JSON - Stack Overflow
Videos
I managed to do this when I was teaching myself JSON and playing with JQ. Now I can't remember how I did this. So any guidance would be of value.
Take the following file: settings.json
{
"Bot_API_Key": "SuperSecretKey",
"Channels": [
{
"Channel_Name": "First Channel",
"Channel_Short": "ch01",
"Channel_ID": 4004841050681
},
{
"Channel_Name": "Second Channel",
"Channel_Short": "ch02",
"Channel_ID": 4004685917007
}
]
}If I use the following:
jq '.Channels[] | contains("ch02")' settings.jsonIt returns:
false true
What I actually need is the index number in the array. In this case it must return 1.
I did achieve this once, a few weeks back, when experimenting, and now I can't repeat the results. Like a fool, I didn't document everything I did.
I think this is fairly easy to achieve for this inner list. The values seem to be all zero's so...
jq '.[][1]' < yourjsonfile
Just to provide another approach. When working with lists, dicts and other types python is the right tool. To give you an idea on how you could retrieve the values from the list you'd do something lik:
#!/usr/bin/env python
mylist = [[1645128660000,0],[1645128720000,0],[1645128780000,0],[1645128840000,0],[1645128900000,0],[1645128960000,0],[1645129020000,0],[1645129080000,0],[1645129140000,0],[1645129200000,0]]
for k,v in mylist:
print("Key":,k)
print("Value":v)
Or using list comprehension
[v for k,v in mylist]
should be sufficient. There is also this awesome page where you can play around with jq: https://jqplay.org/#
If we assume that the input is an array of elements and that each element looks like [key, value] with integer keys and values, then we may extract the value for a given key using the below command:
mykey=1645128900000
jq --argjson key "$mykey" '.[] | select(first == $key) | last' file
This selects all the array entries with the given key as its first element and then extracts the value, the last element, from each piece chosen.
Given the following JSON, what is the best way to extract the phone numbers, whether inside an object or an array of objects?
{
"phones": {
"Alex Baker": { "location": "mobile", "number": "+14157459038" },
"Bob Clarke": [
{ "location": "mobile", "number": "+12135637813" },
{ "location": "office", "number": "+13104443200" }
],
"Carl Davies": [
{ "location": "office", "number": "+14083078372" },
{ "location": "lab", "number": "+15102340052" }
],
"Drew Easton": { "location": "office", "number": "+18057459038" }
}
}I'm using the following query, but I wonder if there's a better way to do this:
$ cat phones.json | jq '.phones | to_entries | [ .[].value | objects | .number ] + [ .[].value | arrays | .[].number ]' [ "+14157459038", "+18057459038", "+12135637813", "+13104443200", "+14083078372", "+15102340052" ]
Any suggestions will be appreciated, thanks!
The command-line tool jq writes to STDOUT and/or STDERR. If you want to write the .AssetId information to STDOUT, then one possibility would be as follows:
jq -r ".[] | .AssetId" input.json
Output:
14462955
114385498
29715011
98253651
A more robust incantation would be: .[] | .AssetId? but your choice will depend on what you want if there is no key named "AssetId".
You can also do it via this command.
jq ".[].AssetId" input.json
if array like be that which is in my case
{
"resultCode":0,
"resultMsg":"SUCCESS",
"uniqueRefNo":"111222333",
"list":[
{
"cardType":"CREDIT CARD",
"isBusinessCard":"N",
"memberName":"Bank A",
"memberNo":10,
"prefixNo":404591
},
{
"cardType":"DEBIT CARD",
"isBusinessCard":"N",
"memberName":"Bank A",
"memberNo":10,
"prefixNo":407814
},
{
"cardType":"CREDIT CARD",
"isBusinessCard":"N",
"memberName":"Bank A",
"memberNo":10,
"prefixNo":413226
}
]
}
you can get the prefixNo with below jq command.
jq ".list[].prefixNo" input.json
For more specific case on array iterating on jq you can check this blogpost
You can use to_entries, then select the non-null keys and extract the values:
jq 'to_entries | map(select(.key != "null"))[] | (.key, .value.type)' < file2.json
Similar idea as in another answer but acting on the original JSON document without transforming the name keys' values into keys:
$ jq -r 'map( select(.name != "null") | .name, .type )[]' file
type1
string
type2
string
type4
string
This extracts all the entries in the top-level array whose name values are not the string null. It then pulls out the name and type values from the remaining entries.
In general, it makes little sense to use data as the value of keys in JSON, which is why I opted for working with the original file.
You need to combine filters by means of | operator:
$ jq -r '.[] | .[] | .name' test.json
rhel6.6
rhel7
The first .[] fetches repositories array. The next .[] fetches all the items of the repositories array. Finally, .name extracts properties from the array items(objects).
Note, the first .[] works on object because it is a documented feature:
.[]
If you use the .[index] syntax, but omit the index entirely, it
will return all of the elements of an array...
You can also use this on an object, and it will return all the
values of the object.
You want to look at the repositories array instead of treating the input as an array:
$ jq -r '.repositories[].name' file
rhel6.6
rhel7