Evidently one of the items in the array is a string. If your jq supports "?", then one possibility would be to use it:
.response[].text?
Another would be to check the type explicitly, e.g.:
.response[] | objects | .text
Yet another possibility:
.response[] | select(type=="object" and has("text")) | .text
If you want to have a placeholder value when there is no "text" field:
.response[] | if type=="object" and has("text") then .text else null end
So it really depends on your requirements and perhaps the version of jq that you are using.
Answer from peak on Stack Overflowshell - Get value of JSON [] array in jq - Unix & Linux Stack Exchange
Using JQ to return the index number where an element in an array has a specific value.
How can I get jq to produce the array of values of a json dictionary? - Unix & Linux Stack Exchange
jq: Extract element from object or array of objects
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.
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.