jq: error (at :0): Cannot iterate over null (null)

To protect against the possibility that .items is not an array, you could write:

.items | .[]?

or even more robustly:

try .items[]

which is equivalent to (.items[])?.

In summary:

  • try E is equivalent to try E catch empty
  • try E is equivalent to (E)?

(Note that the expressions .items[]? and (.items[])? are not identical.)

However none of these will provide protection against input that is invalid JSON.


p.s. In future, please follow the mcve guidelines (http://stackoverflow.com/help/mcve); in the present case, it would have helped if you had provided an illustrative JSON snippet based on the output produced by the curl command.

Leave a Comment