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.

>Solution :

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

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


Leave a Reply Cancel reply