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
Discussions

Validating JSON files on Ubuntu from the command line - Software Recommendations Stack Exchange
I'm looking for an offline command line program that can validate JSON files on Ubuntu: I have a folder containing a list of JSON files, some possibly corrupted, so I would have to have the list of More on softwarerecs.stackexchange.com
🌐 softwarerecs.stackexchange.com
linux - How to validate a json to make sure each json object is valid? - Unix & Linux Stack Exchange
I have a json as shown below where I have multiple json objects inside rootLevel. I need to validate my json to make sure each json object should follow this below rules: Value in clientId, procId... More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 16, 2020
Validate JSON from the command line on Linux
This might be semantics or a bit nit-picky but I dont like these "xxxx from the command line on Linux" when it actually requires you to install a python package through the python package manager or really do anything that isnt on a stock OS unless it realistically needs extra stuff installed (like running a web server). This is 100% irrelevant for any infrastructure in an enterprise network that has firewall restrictions and anything of a policy on not being able to install such things. Doing things "from the linux command line" to me are using things at your most base available system. Like if you want to do a TCP port check but dont have netcat or telnet or something? Easy! timeout 3 bash -c "echo 1 > /dev/tcp/google.com/80" && echo "+ Port opened!" || echo "! Port NOT opened" This to me is the definition of "doing something from the command line" EDIT: Its obvious my point was missed More on reddit.com
🌐 r/linux
5
5
October 3, 2020
Looking for a way to validate json file.
can't you simply try convertfrom-json and catch if it fails? More on reddit.com
🌐 r/PowerShell
7
5
November 21, 2019
🌐
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 - How to parse JSON string via command line on Linux · How to access Linux command cheat sheets from the command line ... Copyright © 2026 The Linux Foundation®. All rights reserved. The Linux Foundation has registered trademarks and uses trademarks. For a list of trademarks of The Linux Foundation, please see our Trademark Usage page. Linux is a registered trademark of Linus Torvalds.
🌐
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.
🌐
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
🌐
JSON Type Definition
jsontypedef.com › docs › jtd-validate
Validating JSON data in shell scripts with jtd-validate
First, to clarify terminology: a shell is the command language you write in when you’re using a terminal. Popular shells include bash and zsh. Shells are programming languages that specialized for running CLI tools and capturing and redirecting their input/output. 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...
🌐
Xmodulo
xmodulo.com › validate-json-command-line-linux.html
How to validate JSON from the command line on Linux
November 25, 2020 - To run it from the command line, you can download its fully executable JAR version from Bintray. Make sure to download json-schema-validator-X.X.X-lib.jar, which has all dependencies incorporated.
Find elsewhere
🌐
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.
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
🌐
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
🌐
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%
🌐
Nidkil
nidkil.me › 2018 › 02 › 23 › validating-a-json-file-from-the-command-line-in-linux
Validating a json file from the command line in Linux | Adventures of a space monkey
February 23, 2018 - So I needed to strip the comments to valiate the file. After googeling around a bit I found an awk command to do just this. Try the following command. awk '{sub(/\/.*$/,"")}1' settings.analyzer.json > settings.analyzer.json · Now run jsonlint again and it should work. P.s. I know the title of this post says Linux, but jsonlint will also work under any other system that nodejs runs on.
🌐
#!/bin/bash
shscripts.com › home › check if json is valid
check if json is valid - #!/bin/bash
March 18, 2024 - Validating them is easy, just check the exit code of the following command, like: [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 ...
🌐
GitHub
github.com › DevelopersToolbox › validate-json
GitHub - DevelopersToolbox/validate-json: A bash script that will allow you to validate a JSON string. · GitHub
Verbose mode will display messages ... use return codes for success (0) and failure (1). source validate-json.sh validate_json_from_file "<filename>"...
Author   DevelopersToolbox
🌐
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
🌐
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: ` jq < file.json` 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/ This alternative ...
🌐
Reddit
reddit.com › r/linux › validate json from the command line on linux
r/linux on Reddit: Validate JSON from the command line on Linux
October 3, 2020 - Doing things "from the linux command line" to me are using things at your most base available system. Like if you want to do a TCP port check but dont have netcat or telnet or something? Easy! timeout 3 bash -c "echo 1 > /dev/tcp/google.com/80" && echo "+ Port opened!" || echo "! Port NOT opened" This to me is the definition of "doing something from the command line" ... I was excited to read this because I need a json validation method from the command line.
🌐
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 ·