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
🌐
GitHub
github.com › martinlindhe › validjson
GitHub - martinlindhe/validjson: Command line tool to validate JSON syntax of input file.
Command line tool to validate and pretty-print JSON syntax of input files.
Author   martinlindhe
🌐
Json-buddy
json-buddy.com › json-validator-command-line-tool.htm
Free JSON validator command-line tool for Windows
The command-line tool (valbuddy.exe) provides a versatile solution for automated validation and processing of XML and JSON data on Windows®. It supports JSON Schema validation, W3C XML checks, pretty-printing, minification, and batch operations — all accessible from the command prompt or scripts.
🌐
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.
🌐
#!/bin/bash
shscripts.com › home › check if json is valid
check if json is valid - #!/bin/bash
March 18, 2024 - [root@linux ~]# [[ $(jq -e . < good.json &>/dev/null; echo $?) -eq 0 ]] && echo "json is valid" || echo "json not valid" json is valid [root@linux ~]# [[ $(jq -e . < bad.json &>/dev/null; echo $?) -eq 0 ]] && echo "json is valid" || echo "json not valid" json not valid [root@linux ~]# When used with “-e”, jq sets the exit status of jq to 0 if the last output values was neither false nor null. It sets it to 1 if the last output value was either false or null, or 4 if no valid result was ever produced.
🌐
GitHub
github.com › zaach › jsonlint
GitHub - zaach/jsonlint: A JSON parser and validator with a CLI. · GitHub
Install jsonlint with npm to use the command line interface: 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 ...
Starred by 2K users
Forked by 415 users
Languages   JavaScript 91.2% | HTML 3.8% | Yacc 3.0% | Lex 1.1% | Makefile 0.9%
Find elsewhere
🌐
Linux Hint
linuxhint.com › validate-json-files-from-command-line-linux
How to Validate JSON from the Command Line on Linux – Linux Hint
Validating JSON from the command line can be done using JSON Spec, jq and jsonlint. These tools can validate the JSON data, providing feedback on any errors.
🌐
Ubuntu
manpages.ubuntu.com › manpages › bionic › 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 --verbose Show additional output --quiet Suppress all output -h --help Show this help validate-json 5.2.6 October 2017
🌐
JSON Type Definition
jsontypedef.com › docs › jtd-validate
Validating JSON data in shell scripts with jtd-validate
jtd-validate is a CLI tool that can validate JSON input against a JSON Typedef schema. It lives on GitHub here. This article will go over why jtd-validate may be useful to you, how to install it, and then go through an example of using jtd-validate ...
🌐
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
🌐
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.
🌐
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.
🌐
Commandlinefu
commandlinefu.com › commands › view › 24247 › use-jq-to-validate-and-pretty-print-json-output
use jq to validate and pretty-print json output Using cat
the `jq` tool can also be used do validate json files and pretty print output `cat file.json | jq` available on several platforms, including newer debian-based systems via `#sudo apt install jq`, mac via `brew install jq`, and from source https://stedolan.github.io/jq/download/ ... success: ...
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
🌐
npm
npmjs.com › package › valid-json-cli
valid-json-cli - npm
April 7, 2020 - Usage: validjson path [options] cat file.json | validjson [options] validjson [options] < file.json Options: -s, --silent no text output - will still exit with exitcode 0 or 1 -v, --version display version number and exit -h, --help display this help and exit · Currently the only option which does something is --silent, which supresses error hint on error. It does not matter if you set the option before or after the path if you supply a file as parameter. Unknown parameters are ignored. The difference between validjson file.json and validjson < file.json ...
      » npm install valid-json-cli
    
Published   Apr 07, 2020
Version   1.4.1
Author   dotnetcarpenter
🌐
Commandlinefu
commandlinefu.com › commands › view › 5074 › validate-json
validate json
March 16, 2010 - curl -s -X POST http://www.jsonlint.com/ajax/validate -d json=\"`cat file.js`\" -d reformat=no - (validate json I have this saved as jsonlint chmodded +x and file.js is instead $1, but YMMV). The best command line collection on the internet, submit yours and save your favorites.
🌐
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.