24

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!

4 Answers 4

20

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.

4
  • What exactly jsonlint will do, please advise Jan 27, 2017 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. Nov 28, 2018 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. Jun 24, 2021 at 21:57
  • @DanielMcLaury Note that you can type jsonlint+<tab> and it will finish and add the -php for you. Mar 1 at 0:02
8

I tried jsonlint but it doesn't work.

jq . may-file.json work nice!

Hope this feedback is helpful.

3
  • 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> Aug 13, 2020 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, 2020 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, 2021 at 16:22
5

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.

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
5
  • But jq doesn't detect this incomplete document jq '.' <<<'{"foo'
    – Joe
    Aug 6, 2018 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, 2018 at 12:37
  • Yes. jsonlint (the javascript version available at jsonlint.com), for example, reports an error for that incomplete document.
    – Joe
    Aug 7, 2018 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, 2020 at 3:59
  • It says: Input error: Is a directory when I run jq . package.json. Is it a normal behavior?
    – alper
    Mar 22 at 12:35

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.