Ask Your Question
0

Is it possible to retrieve objects as a list in jq in order to access them by index?

asked 2022-10-17 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-03-25 23:00:00 +0000

nofretete gravatar image

Yes, it is possible to retrieve objects as a list in jq and access them by index using the built-in keys function to get the keys of the object and then using the .[index] operator to access the values associated with those keys. Here's an example:

Suppose you have the following JSON object:

{
  "foo": "bar",
  "baz": 42,
  "qux": true
}

You can retrieve the values of this object as a list using the following jq query:

$ jq '[.[]]' example.json

This will output the values of the object as a list:

[
  "bar",
  42,
  true
]

You can then access individual values in this list by index using the .[index] operator. For example, to access the second element in the list (which has an index of 1), you can use the following jq query:

$ jq '[.[]][1]' example.json

This will output:

42
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-10-17 11:00:00 +0000

Seen: 21 times

Last updated: Mar 25 '22