I have yet to find a system where python -mjson.tool doesn't work. So you can do:

python -mjson.tool "$somefile" > /dev/null

The exit code will be nonzero and you get the parse error on stderr if the file is not valid JSON.

Note: The Python libraries don't follow the JSON spec and allow NaN and Infinity as values. So using json.tool will let some errors slip through. Still it's good enough for my use case of catching errors in human-written documents early.

Answer from sba on Stack Overflow
🌐
Linux Hint
linuxhint.com › validate-json-files-from-command-line-linux
How to Validate JSON from the Command Line on Linux – Linux Hint
After installing pip on Linux, run the following command to install JSON Spec. ... Now we will use two JSON files named data.json and schema.json. Using the JSON Spec python library tool, we will validate our JSON files.
🌐
Xmodulo
xmodulo.com › validate-json-command-line-linux.html
How to validate JSON from the command line on Linux
November 25, 2020 - This tutorial describes how to validate JSON data and JSON schema from the command line on Linux.
🌐
Linux.com
linux.com › home › training and tutorials › how to validate json from the command line on linux
How to validate JSON from the command line on Linux - Linux.com
March 29, 2016 - Due to its syntactic simplicity and flexibility, JSON (JavaScript Object Notation) has become pretty much the de-facto standard data exchange format in many web applications. As JSON becomes widely used to represent structured data with a great degree of flexibility, the need arises for being able to “validate” a JSON representation.
🌐
#!/bin/bash
shscripts.com › home › check if json is valid
check if json is valid - #!/bin/bash
March 18, 2024 - < bad.json &>/dev/null; echo $?) -eq 0 ]] && echo "json is valid" || echo "json not valid" json not valid [root@linux ~]#
🌐
JSON Type Definition
jsontypedef.com › docs › jtd-validate
Validating JSON data in shell scripts with jtd-validate
jtd-validate makes it easy to validate JSON from a shell script. This is useful in situations where you’re usually forced to write a shell script anyway, such as in many configuration files, such as a Dockerfile or Makefile, or in the ...
Find elsewhere
🌐
Baeldung
baeldung.com › home › scripting › parsing, validating, and printing json in shell scripts
Parsing, Validating, and Printing JSON in Shell Scripts | Baeldung on Linux
March 18, 2024 - The well-documented and open-source processor jq is a lightweight way to parse, validate, and generally deal with the JSON format. It accepts both file and stdin as its input and always performs validations.
🌐
GitHub
github.com › martinlindhe › validjson
GitHub - martinlindhe/validjson: Command line tool to validate JSON syntax of input file.
$ validjson file.json OK: file.json $ curl http://site.com/file.json | validjson OK: -
Author   martinlindhe
Top answer
1 of 2
2

Without the need for a specific application, if you just want to separate the wheat from the chaff, and have PHP installed on the Linux machine, you can use a simple one-liner for this:

for json in folder/*; do php -r "if ( ! \$foo=json_decode(file_get_contents('$json')) ) echo \"$json\n\";"; done

This would list all broken .json files in the directory named folder. Take out the exlamation mark (!) to only list the .json files which are fine.

It might well be PHP spits out some additional error message here. If that happens, just re-direct error output (STDERR) to the machine's black hole (/dev/null):

for json in folder/*; do php -r "if ( ! \$foo=json_decode(file_get_contents('$json')) ) echo \"$json\n\";" 2>/dev/null; done

TL;DR – a short explanation of what that one-liner does

  • for json in folder/*; do […] done: loop over all .json files in the given location. On each iteration, store the file name into a variable named $json.
  • php -r: invoke the PHP executable. The -r parameter tells it to (r)un a command instead of expecting a file. The command must directly follow this, separated by white-space.
  • "[…]": using double-quotes, we can easier integrate shell variables. The price is we have to escape all $ signs belonging to PHP variables.
  • \$foo: see previous point, we need to escape the $ here.
  • $foo=json_decode(file_get_contents('$json')): have PHP reading the file (file_get_contents; the $json here is our Bash variable from the first bullet-point), then convert the JSON to a PHP object (json_decode). We don't want to see the result, so we assign it to a variable ($foo in my example).
    If decoding was successful, the assignment will return TRUE, otherwise FALSE – which is why we can use this construction as our "if condition".
  • if ( […] ) echo \"$json\n\";": only write the file name (this again is our Bash variable here) to the console if our condition is met – to "separate the wheat from the chaff", as I've put it initially. As usual in PHP, the command needs to be terminated by a semi-colon (;). Also note that I needed to escape the double-quotes here, so our "outer Shell wrapper" doesn't end prematurely. I needed the double-quotes, so \n is interpreted as "new line" – with single-quotes, it would print a literal \n instead.
  • 2>/dev/null: Redirect error output (STDERR) to the "black hole" (i.e. don't show any errors if they occur). Our echo commands go to STDOUT, and thus are not affected by this.
2 of 2
0

You can use jsonlint:

  • CLI
  • open source (written in JavaScript)
  • check whether file(s) are valid:

Example:

# Install
sudo apt-get install -y nodejs npm
sudo npm install jsonlint -g
sudo ln -s /usr/bin/nodejs /usr/bin/node # to avoid "/usr/bin/env: node: No such file or directory" error
jsonlint -qc *.json

# Use
ubuntu@pm:~/pubmed/pubmed$ jsonlint --compact --validate *.json
my_file_bad.json: line 279916, col 18, found: 'EOF' - expected: 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['.
ubuntu@pm:~/pubmed/pubmed$

Useful options:

   -c, --compact            compact error display
   -V, --validate           a JSON schema to use for validation
🌐
Medium
pavolkutaj.medium.com › how-to-check-the-validity-of-json-with-jq-in-bash-scripts-21523418f67d
How To Check the Validity of JSON with jq in bash scripts | by Pavol Z. Kutaj | Medium
February 21, 2024 - json_data='{"key": "value", "invalid_key": "missing_quote"}' set +e retval=$(jq -re '""' <<<"${json_data}" 2>&1) if [ -z "${retval}" ]; then echo "JSON parsing successful" else echo "ERROR: jq - ${retval}" fi # >>> ERROR: jq - jq: parse error: Unfinished string at EOF at line 2, column 0
🌐
Red Hat
access.redhat.com › solutions › 6955301
Is there any tool to validate JSON file format ? - Red Hat Customer Portal
Is there anyway to validate the format of JSON file ? Red Hat Enterprise Linux 8, 9 · yajl · A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more. Log in for full access Log In · Learn more about Red Hat subscriptions ·
🌐
Arch Linux Man Pages
man.archlinux.org › man › json-glib-validate.1.en
json-glib-validate(1) — Arch manual pages
json-glib-validate offers a simple command line interface to validate JSON data. It lets you list URIs that point to JSON data and checks that the data conforms to the JSON syntax. The resources to operate on are specified by the FILE argument.
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
The best way to find and correct errors while simultaneously saving time is to use an online tool such as JSONLint. JSONLint will check the validity of your JSON code, detect and point out line numbers of the code containing errors.
🌐
Ubuntu
manpages.ubuntu.com › manpages › xenial › man1 › validate-json.1.html
Ubuntu Manpage: validate-json - JSON Schema command line interface
--dump-schema Output full schema and exit --dump-schema-url Output URL of schema -h --help Show this help validate-json 1.6.1 March 2016
🌐
GitHub
github.com › DevelopersToolbox › validate-json
GitHub - DevelopersToolbox/validate-json: A bash script that will allow you to validate a JSON string. · GitHub
silence_messages=true source validate-json.sh if validate_json_from_file "<filename>"; then go_perform_some_action_on_data else do_something_else_like_raise_an_error fi
Author   DevelopersToolbox
🌐
Ubuntu
manpages.ubuntu.com › manpages › trusty › man1 › jsonlint.1.html
Ubuntu Manpage: jsonlint - A JSON syntax validator and formatter tool
Options are: -v, -s, -S, -f, -F, -e -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 -F, --format-compactly Reformat the JSON simlar to -f, but do so compactly by removing all unnecessary whitespace -e codec, --encoding=codec --input-encoding=codec --output-encoding=codec Set the input and output character encoding codec (e.g., ascii, utf8, utf-16). The -e will set both the input and output encodings to the same thing. If not supplied, the input encoding is guessed according to the JSON specification.
🌐
GitHub
github.com › zaach › jsonlint
GitHub - zaach/jsonlint: A JSON parser and validator with a CLI. · GitHub
npm install jsonlint -g · Validate a file like so: jsonlint myfile.json · or pipe input into stdin: cat myfile.json | jsonlint · jsonlint will either report a syntax error with details or pretty print the source if it is valid.
Starred by 2K users
Forked by 416 users
Languages   JavaScript 91.2% | HTML 3.8% | Yacc 3.0% | Lex 1.1% | Makefile 0.9%