13

I have tried to get hands on some JSON file validators. I came across jq. But after running 'jq . file.json' I got JSON formatted output. That command didn't gave me validation of JSON in my file.
I want to know how I can check the syntax or validate JSON format in my file in Ubuntu. Please advise me.

11

Try jsonlint:

sudo apt install jsonlint

The basic usage syntax is

jsonlint YOUR-FILE.JSON

You find its manual by typing man jsonlint or visiting its online manpage:

An excerpt:

NAME
       jsonlint - A JSON syntax validator and formatter tool

SYNOPSIS
       jsonlint [-v][-s|-S][-f|-F][-ecodec]inputfile.json...

[...]

OPTIONS
       The  return  status  will  be  0 if the file is legal JSON, or non-zero
       otherwise.  Use -v to see the warning details.

       [...]

       -v, --verbose
              Show details of lint checking
       -s, --strict
              Be strict in what is considered legal JSON (the default)
       -S, --nonstrict
              Be loose in what is considered legal JSON
       -f, --format
              Reformat the JSON (if legal) to stdout

[...]

So you can see whether your JSON is valid by checking the return code of jsonlint. You can see it by running echo $? right afterwards (0=OK, 1=invalid), or by evaluating it using &&, || or if.

| improve this answer | |
  • What exactly jsonlint will do, please advise – Jaffer Wilson Jan 27 '17 at 10:11
  • 8
    When I installed the jsonlint package on Ubuntu just now, the installed binary was for some reason called jsonlint-php rather than jsonlint. It appears to work correctly, but I had to remember the dpkg-query -L command to figure out what it had actually installed. – Daniel McLaury Nov 28 '18 at 17:39
2

jq will spit out the error explicitly, and you can also check the exit status, which is 1 for parse errors, and obviously 0 for successes.

For example:

% jq '.' <<<'{"foo": "spam", "bar": 1}'
{
  "bar": 1,
  "foo": "spam"
}

% echo $?
0

Now, let's replace : with = after "bar"-- making the input an invalid json:

% jq '.' <<<'{"foo": "spam", "bar"= 1}'
parse error: Invalid numeric literal at line 1, column 23

% echo $?                                  
1
| improve this answer | |
  • But jq doesn't detect this incomplete document jq '.' <<<'{"foo' – Joe Aug 6 '18 at 19:21
  • @Joe It's not showing anything on output expectedly. Did you mean, it should raise an error or something instead? – heemayl Aug 7 '18 at 12:37
  • Yes. jsonlint (the javascript version available at jsonlint.com), for example, reports an error for that incomplete document. – Joe Aug 7 '18 at 23:06
  • jq sucks, a valid json file will report "jq: error: syntax error, unexpected '/', expecting $end (Unix shell quoting issues?) at <top-level>, line 1:" – Liu Hao Jan 18 at 3:59
2

You can do this using python json.tool module

echo '{"name": "dedunu", "country": "LKA"}' | python -m json.tool

If you have a file you can use it as below.

python -m json.tool file.json

But the problem with this command is that you won't get a detail about the problem in JSON file. I found the answer from this link.

| improve this answer | |
0

I tried jsonlint but it's doesn't work.

jq . may-file.json work nice!

Hope this feedback is helpful.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.