Jq: is it possible to use jq purely to tell me if the input file is valid or not?

Created on 29 Mar 2018  路  12Comments  路  Source: stedolan/jq

I have a pretty specific usecase, but I think it would be valuable for the tool itself.

I want to know if it's possible to do something like this.

$ cat file.json | jq --validate 
# output message or only output on error. 

I think the ability to validate a particular file to make sure it's valid json without printing the contents to the command line would be incredibly valuable.

Please let me know if this is possible and i'm missing something. Thanks!

Most helpful comment

Just for future reference:

if [ $(echo '{"test":true}' | jq empty > /dev/null 2>&1; echo $?) -eq 0 ]; then
  echo "JSON is valid"
else
  echo "JSON is invalid"
fi

All 12 comments

is it possible to use jq purely to tell me if the input file is valid or not?

Please note, firstly, that jq does not have a "strict" mode, and although jq is fairly strict, jq by design is somewhat lenient. (I've noticed recently that even the JSON validators have disagreements amongst themselves on really important issues, such as duplicate keys.)

Second, jq does not have a "quiet" mode that might help here, but in practice there are two good workarounds: (1) pipe to /dev/null; (2) use a jq filter such as empty, the point being that jq will attempt to parse the input before evaluating the given filter.

For example:

$ echo 'abc' | jq empty
parse error: Invalid numeric literal at line 2, column 0
$ echo $?
4

Third, jq can often be helpful in giving information about multiple errors. For example, if the input is intended to conform with JSON-Lines (one JSON entity per line), then consider this example:

(echo abc; echo [1]; echo def) | jq -R -n 'inputs | try (fromjson|empty) catch input_line_number'
1
3

using empty works the way I need it to. Thanks!

jq empty does not catch all invalid jsons:
Good JSON:
`# echo '{"test":"val"}' | jq empty; echo $?

0
`

Bad JSON (missing closing brace)
`# echo '{"test":"val"' | jq empty; echo $?

0
`

Bad JSON (extra closing brace)
`# echo '{"test":"val"}}' | jq empty; echo $?

parse error: Unmatched '}' at line 1, column 15
4
`

jq --version

jq-1.5

Using jq 1.5, one would have to check the contents of STDERR.

Using jq 1.6, the -e option can be used:

echo '[1' | jq-1.6  empty ; echo $?
parse error: Unfinished JSON term at EOF at line 2, column 0
4

Of course this comes with the caveat that jq does not currently have a "strict" parsing mode.

Just for future reference:

if [ $(echo '{"test":true}' | jq empty > /dev/null 2>&1; echo $?) -eq 0 ]; then
  echo "JSON is valid"
else
  echo "JSON is invalid"
fi

If you are testing a JSON file:

if jq empty file.json; then
  echo "JSON is valid"
else
  echo "JSON is invalid"
fi

If you don't care about the error output:

if jq empty file.json 2>/dev/null; then
  echo "JSON is valid"
else
  echo "JSON is invalid"
fi

Bad JSON (missing closing brace)
`# echo '{"test":"val"' | jq empty; echo $?

0
`

Here it's working:

$ jq --version
jq-1.6
$ echo '{"test":"val"' | jq empty; echo $?
parse error: Unfinished JSON term at EOF at line 2, column 0
4

Beware that jq empty tells that this is valid JSON:

{}{}

But this is not a strictly valid JSON string, it is a concatenation of two JSON strings

@jakutis - jq is stream-oriented. Any stream of valid JSON values (including an empty stream) will be viewed by jq as valid.

Yes, even empty file would be valid for jq. Added my comment for people who would go ahead and use jq as a json file linter, which would be a mistake

FYI, this seems to be a duplicate of https://github.com/stedolan/jq/issues/1539

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rclod picture rclod  路  4Comments

lhunath picture lhunath  路  3Comments

thelonious picture thelonious  路  4Comments

ghost picture ghost  路  4Comments

thedward picture thedward  路  3Comments