Sanket Kankariya
Sanket Kankariya

Reputation: 289

jq accepts invalid json input when it should throw an error

I am having problem to validate json string. i am using below code

if jq -e . >/dev/null 2>&1 <<<"$json_string"; then
        echo "Parsed JSON successfully and got something other than false/null"
    else
        echo "Failed to parse JSON, or got false/null"
    fi

This does not work for json_string={"fruit":{"name":"app. this still shows Parsed JSON successfully and got something other than false/null where as the json string is incomplete.

Upvotes: 3

Views: 9570

Answers (1)

Inian
Inian

Reputation: 85530

Apparently it is one of the issues in jq-1.5. Un-terminated objects/arrays, without a corresponding close character, are being treated as valid objects and are accepted by the parser. Can reproduce in jq-1.5, but fixed in jq-1.6

On jq-1.6

jq -e . <<< '{"fruit":{"name":"app'
parse error: Unfinished string at EOF at line 2, column 0
echo $?
4

minimal reproducible example below, which again is handled well in 1.6 but doesn't throw an error in 1.5

jq -e . <<< '{'
parse error: Unfinished JSON term at EOF at line 2, column 0
jq -e . <<< '['
parse error: Unfinished JSON term at EOF at line 2, column 0

Suggest upgrading to jq-1.6 to make this work!

Upvotes: 4

Related Questions