0

How do you get the value of inside of an [key,value] array from a json output with jq

[[1645128660000,0],[1645128720000,0],[1645128780000,0],[1645128840000,0],[1645128900000,0],[1645128960000,0],[1645129020000,0],[1645129080000,0],[1645129140000,0],[1645129200000,0]]
2
  • 1
    Hi and Welcome to SE. Usually you'll post the input, what your expected output should be and what you tried. I think this is fairly easy to achieve for this inner list. The values seem to be all zero's so... jq '.[]|.[1]' < yourjsonfile should be sufficient. There is also this awesome page where you can play around with jq: jqplay.org/# Feb 17, 2022 at 21:11
  • 1
    @roaima and so I did. :). Thanks Feb 18, 2022 at 8:07

2 Answers 2

2

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/#

0
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .