Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select key:value with jq and output as array

When using jq it is easy to select a key:value that you want to output, but by default the array dissapears. How do I keep the output as an array?

Example

# My JSON
my_json='{"count":2,"values":[{"name": "Hans","age": 22},{"name": "John","age": 88}]}'

# Getting names
echo $my_json | jq ".values[].name"
"Hans"
"John"

What I want

["Hans", "John"]

With an output like that I can e.g. remove the quotes by outputting it to tsv.

# Output to tsv
echo '["Hans", "John"]' | jq -r '.[]'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add square brackets along with c(ompact output) option such as

echo $my_json | jq -c '[.values[].name]'

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading