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

jq: output structure, shortening array, preserving the other fields in the input

For a JSON file like this:

{
  "mylist": [...],
  "foo": "bar"
}

with mylist containing many elements, how can I output the same JSON structure, but with only the first N elements in mylist?

I was trying to use jq '.mylist[:N]', but this prints the first N elements of mylist without the other fields such as foo.

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 :

Instead of re-creating the whole object again, you could just update |= that one field:

jq '.mylist |= .[:N]'

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