27

I am trying to get my hands on some JSON file validators.

I came across jq but after running jq . file.json I only got JSON formatted output, not a validation of the 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!

Zanna
  • 72,471

4 Answers4

21

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.

Byte Commander
  • 110,523
  • What exactly jsonlint will do, please advise – Jaffer Wilson Jan 27 '17 at 10:11
  • 16
    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
  • 1
    It installed as jsonlint-php for me as well. Also, the call it expects is a little unusual, with the commands at the end. You use jsonlint-php file.json -v for verbose outputs, for example. – Stella Biderman Jun 24 '21 at 21:57
  • @DanielMcLaury Note that you can type jsonlint+<tab> and it will finish and add the -php for you. – Alexis Wilke Mar 01 '22 at 00:02
11

I tried jsonlint but it doesn't work.

jq . may-file.json work nice!

Hope this feedback is helpful.

zatamine
  • 564
  • Note: to install jsonlint you need to run sudo apt install jsonlint, but to run the program itself, the usage is: jsonlint-php <file.json> – Olivia Stork Aug 13 '20 at 15:19
  • Thank you, but : When you install jsonlint you install all this dependencies php-cli php-common php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-readline (maybe php-pear too) and you need to get 2,519 kB , when you install jq you install this dependencies libjq1 libonig4 and you need to get 276 kB.

    Each one has its own preferences :)

    – zatamine Aug 14 '20 at 12:12
  • 3
    Recommending jq over jsonlint for large JSON files. It was more than 10× faster reading, analyzing and prettyprinting a 600 MiB JSON file in my test. – tanius Nov 25 '21 at 16:22
6

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.

dedunu
  • 9,586
  • Looks like json.tool will return an error code of 0 if the file is valid JSON and 1 for invalid JSON. If you want to not print the output, the easiest way I know is to use /dev/null as the output. – mwfearnley Apr 04 '23 at 14:58
4

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
heemayl
  • 94,145
  • But jq doesn't detect this incomplete document jq '.' <<<'{"foo' – Joe Aug 06 '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 07 '18 at 12:37
  • Yes. jsonlint (the javascript version available at jsonlint.com), for example, reports an error for that incomplete document. – Joe Aug 07 '18 at 23:06
  • jq sucks, a valid json file will report "jq: error: syntax error, unexpected '/', expecting $end (Unix shell quoting issues?) at , line 1:" – Liu Hao Jan 18 '20 at 03:59
  • It says: Input error: Is a directory when I run jq . package.json. Is it a normal behavior? – alper Mar 22 '22 at 12:35