JSON wrangling with jq
Help 4 / 10
Filter data

Putting Elements in an Array

Once you start using the array index to select elements, you have a new problem. The data returned won’t be a valid JSON document. In the example above, the issue titles were new line delimited:

jq '.[].title' issues.json

In fact, whenever you ask jq to return an unwrapped collection of elements, it prints them each on a new line. You can see this by explicitly asking jq to ignore its input and instead return two numbers:

echo '""' | jq '1,2'

You can resolve this the same way you would turn the text 1,2 into an array in JavaScript: By wrapping it in an array constructor [ ... ].

echo '""' | jq '[1,2]'

Similarly, to put a generated collection of results into a JSON array, you wrap it in an array constructor [ ... ].

My GitHub issue title filter (.[].title) then becomes [ .[].title ] like this:

jq '[ .[].title ]' issues.json

Now I have a valid JSON document.

Loading...