Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jq: error (at <stdin>:0): Cannot iterate over null (null)

Tags:

jq

I've been working with an API call to structure it in JSON format so I might later push it into a database. Then code looks like this:

getPage() {
curl --fail -X GET 'https://api.app.com/v1/test?page=1&pageSize=1000&sort=desc' \
  -H 'Authorization: Bearer 123abc456pickupsticks789' \
  -H 'cache-control: no-cache'  
}

getPage \
| jq -c '.items | .[] | {landing_id: .landing_id, submitted_at: .submitted_at, answers: .answers, email: .hidden.email}' \
  > testpush.json

When I run it though, it produces this error: jq: error (at <stdin>:0): Cannot iterate over null (null)

I've looked at solutions such as this one, or this one from this site, and this response.

The common solution seemed to be using a ? in front of [] and I tried it in the jq line towards the bottom, but it still does not work. It just produces an empty json file.

Am I misreading the takeaway from those other answers and not putting my ? in the right place?>

like image 918
gooponyagrinch Avatar asked Feb 20 '19 20:02

gooponyagrinch


1 Answers

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.

like image 144
peak Avatar answered Sep 18 '22 14:09

peak