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
🌐
Homebrew
formulae.brew.sh › formula › jsonlint
jsonlint — Homebrew Formulae
Install command: brew install jsonlint · JSON parser and validator with a CLI · https://github.com/zaach/jsonlint · License: MIT · Development: Pull requests · Formula JSON API: /api/formula/jsonlint.json · Formula code: jsonlint.rb on GitHub · Bottle (binary package) installation support provided.
🌐
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. Windows and macOS binaries are available under Releases
Author   martinlindhe
🌐
JSON Type Definition
jsontypedef.com › docs › jtd-validate
Validating JSON data in shell scripts with jtd-validate
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 configuration for your testing or CI/CD infrastructure (e.g. Jenkins or GitHub Actions). If you want to validate JSON data from within a program written in a “traditional” (non-Shell) programming language, using jtd-validate is not recommended. Instead, use an implementation for the language you’re working in. If you’re on macOS, the easiest way to install jtd-validate is via Homebrew:
🌐
MacBlog
macblog.org › parse-json-command-line-mac
How to Parse JSON on the macOS Command Line Without External Tools Using JavaScript for Automation - MacBlog
November 24, 2021 - JXA is built right into macOS and lets you use JavaScript without any additional tooling. You may be familiar with using the osascript binary to execute AppleScript snippets. While AppleScript is a more common choice for accessing OSA functionality, you can trivially use JavaScript instead. On the command line, use the -l (that's a lowercase L) flag with osascript to specify JavaScript as the scripting language.
🌐
GitHub
github.com › atomicbird › jsonlint
GitHub - atomicbird/jsonlint: JSON command-line parsing and checking for Mac OS X
jsonlint is a command-line tool for Mac OS X that can be used to parse, validate, and convert JSON text files.
Starred by 17 users
Forked by 2 users
Languages   Objective-C 100.0% | Objective-C 100.0%
🌐
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 416 users
Languages   JavaScript 91.2% | HTML 3.8% | Yacc 3.0% | Lex 1.1% | Makefile 0.9%
🌐
#!/bin/bash
shscripts.com › home › check if json is valid
check if json is valid - #!/bin/bash
March 18, 2024 - [root@linux ~]# cat good.json {"name":"John", "age":30, "car":null} [root@linux ~]# jq '.' good.json { "name": "John", "age": 30, "car": null } [root@linux ~]# cat bad.json {"name":"John", "age":30, "car":null [root@linux ~]# jq '.' bad.json parse error: Unfinished JSON term at EOF at line 2, column 0 [root@linux ~]# 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 -e .
Find elsewhere
Top answer
1 of 6
10

Parsing json with just sed is as problematic as parsing HTML - in short: since elements can embed other elements and regex doesn't support recursion, it's essentially impossible to parse correctly with just regexp.

There's a PCRE solution to parsing and validating json here: https://stackoverflow.com/questions/2583472/regex-to-validate-json - I haven't used or tested it so I'll have to take the author's word that it works...but PCRE does a lot of things that just aren't in the basic or extended regexps supported by sed.

In any case, IMO you're better off using perl or python or awk and one of the json parsing libs for those languages, or a specialised json parsing tool - several are mentioned here:

https://stackoverflow.com/questions/3858671/unix-command-line-json-parser

Any of them can be used to extract data from JSON input for use in a shell script. Or you could write your entire program within that language.

For example, piping your json data into python -mjson.tool results in this:

$ echo "JSONDATAHERE" | python -m json.tool
{
    "content_url": "http://files.eeehousenyc.com/1I3Q0Z1E2F3C/CastingBy-v12%20mixed.mov", 
    "created_at": "2012-08-27T20:04:27Z", 
    "deleted_at": null, 
    "download_url": "http://files.eeehousenyc.com/1I3Q0F3C/download/CastingBy-v12%20mixed.mov", 
    "gauge_id": null, 
    "href": "http://my.cl.ly/items/2840", 
    "icon": "http://my.cld.me/images/item-types/video.png", 
    "id": 21462840, 
    "item_type": "video", 
    "name": "CastingBy-v12 mixed.mov", 
    "private": true, 
    "redirect_url": null, 
    "remote_url": "http://f.cl.ly/items/3D0P02b3e3p2I/CastingBy-v12%20mixed.mov", 
    "source": "Cloud/1.5.4 CFNetwork/520.4.3 Darwin/11.4.0 (x86_64) (MacPro5%2C1)", 
    "subscribed": true, 
    "updated_at": "2012-08-27T20:13:38Z", 
    "url": "http://files.housenyc.com/1I3E2F3C", 
    "view_counter": 2
}

which you can then pipe into sed like this:

$ echo "JSONDATAHERE" | python -m json.tool | sed -n -e '/"name":/ s/^.*"\(.*\)".*/\1/p'
CastingBy-v12 mixed.mov

Relying on the greedy nature of regexp, the sed script extracts everything between the second-last " and the last " character on any line containing "name":.

2 of 6
6

This is the expression you are looking for:

sed -e 's/^.*"name":"\([^"]*\)".*$/\1/' infile

It results to:

CastingBy-v12 mixed.mov

In yours there are several errors:

  • In sed only greeding expression can be used: .*? and .+? are incorrect.
  • The + must be escaped.
  • Use [^"]* to avoid that the regular expression matches until last double quotes of the string.
🌐
Paulgalow
paulgalow.com › how-to-work-with-json-api-data-in-macos-shell-scripts
How to work with JSON API data in macOS shell scripts without external dependencies | Paul Galow
November 24, 2021 - Environment variables are a tried and tested method to safely hand over data from one execution environment to another. And this is exactly what we are going to use here. We could export our raw JSON input, but an even simpler and more tightly scoped solution is to use a command-specific environment variable.
🌐
JSON Web Tools
jsonwebtools.com › home › how to validate json
How to Validate JSON: Complete Guide with Tools & Examples (2026)
1 week ago - # GitHub Actions example - name: Validate JSON files run: | for file in $(find . -name "*.json" -not -path "*/node_modules/*"); do python3 -m json.tool "$file" > /dev/null && echo "✅ $file" || echo "❌ $file" done · Free, instant JSON validation with clear error messages and line numbers.
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
It will validate your JSON content according to JS standards, informing you of every human-made error, which happens for a multitude of reasons – one of them being the lack of focus. Using JSONLint, you can quickly find any errors that might've occurred, allowing you to focus more on the rest of your code than on a tiny error itself. You can use a URL and JSONLint will scrape it for JSON and parse it.
🌐
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 - jq is a widely used command-line tool for parsing and manipulating JSON data. Introducing syntax errors in JSON can lead to parsing failures, helping in error testing. Using jq -re '""' allows validation of JSON data without processing its contents.
🌐
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
cat file.json | jq - (use jq to validate and pretty-print json output 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`, ...
🌐
npm
npmjs.com › package › valid-json-cli
valid-json-cli - npm
April 7, 2020 - Simpel command line JSON validator with an API. Support nodejs >=7.0.0. Tested to work in Windows PowerShell and *nix bash terminal. ... 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
      » npm install valid-json-cli
    
Published   Apr 07, 2020
Version   1.4.1
Author   dotnetcarpenter
🌐
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.