user1959609
user1959609

Reputation: 9

jq suppress parsing error for 404 json files

echo $(curl -s -u user:pwd "http://site/file.json" | jq -e -r '.data[]? | select(.state == "Active") | if . == null then "Installing" elif . == "Active" then "Active" else "Installing" end')

Following is the error:

parse error: Invalid numeric literal at line 2, column 0

In a scenario when file.json doesn't exist 404 page is returned and jq is throwing a parsing error. In such case I want to return string "Installing". Tried many things but nothing is working out, please help.

Upvotes: 0

Views: 452

Answers (1)

hek2mgl
hek2mgl

Reputation: 157947

Write the response into a variable:

# -f makes curl return an error in case of HTTP error.
# Check "man curl" on how reliable this is.
response="$(curl -f ...)"
if [ $? -ne 0 ] ; then
    echo "Installing"
else
    jq FILTER <<< "${response}"
fi

Upvotes: 1

Related Questions