Ask Your Question
2

How can jq be used to search for a value and return a scalar?

asked 2022-07-11 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2021-07-09 18:00:00 +0000

plato gravatar image

To search for a value and return a scalar, one can use the jq command with the -r flag to return a raw scalar value. The search can be performed using filters.

For example, to search for the value "foo" in a JSON file and return it as a scalar, one can use the following command:

jq -r '.foo' file.json

This will return the value of "foo" as a plain string.

If the value is nested inside a JSON object, you can also use dot notation to navigate to the desired key:

jq -r '.parent.key' file.json

This will return the value of "key" within the "parent" object as a scalar.

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-07-11 11:00:00 +0000

Seen: 21 times

Last updated: Jul 09 '21