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. Windows and macOS binaries are available under Releases
Author   martinlindhe
🌐
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.
🌐
JSON Type Definition
jsontypedef.com › docs › jtd-validate
Validating JSON data in shell scripts with jtd-validate
`set -x` makes # it so that sh/bash/zsh ... as our "script" whose output we want to # make sure is valid. script_output=$(echo '{"username": "adsf", "bio": 3}') # If "$script_output" isn't valid against the schema, jtd-validate will ...
🌐
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%
🌐
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 - One way to parse JSON on the macOS command line, using only native tooling without external dependencies like jq or similar.
🌐
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: ... $ jsonlint -h Usage: jsonlint [file] [options] file file to parse; otherwise uses stdin Options: -v, --version print version and exit -s, --sort-keys sort object keys -i, --in-place overwrite the file -t CHAR, --indent CHAR character(s) to use for indentation [ ] -c, --compact compact error display -V, --validate a JSON schema to use for validation -e, --environment which specification of JSON Schema the validation file uses [json-schema-draft-03] -q, --quiet do not print the parsed json to STDOUT [false] -p, --pretty-print force pretty printing even if invalid
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.
🌐
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`, ...
🌐
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: ...
🌐
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.
🌐
Der Flounder
derflounder.wordpress.com › 2023 › 04 › 15 › using-the-plutil-command-line-tool-to-work-with-json-on-macos-monterey-and-later
Using the plutil command line tool to work with JSON on macOS Monterey and later | Der Flounder
April 15, 2023 - One of the issues Mac admins may face is working with JSON files as part of shell scripting. There are several solutions to this problem, including using the third-party jq command line tool and Apple's JavaScript for Automation (JXA) interface. For posts on using these solutions, please see the links below: jq: https://sher-chowdhury.medium.com/working-with-json-using-jq-ce06bae5545a https://codeahoy.com/learn/introtobash/ch15/ https://cameronnokes.com/blog/working-with-json-in-bash-using-jq/…
🌐
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 - So how can we protect against this threat? That’s where JSON.parse() comes into play. Using it, we make sure to parse valid JSON only. If the incoming string is not JSON, this method call will throw an error.
🌐
JSONLint
jsonlint.com
JSONLint - The JSON Validator
Using JSONLint, you can quickly ... it for JSON and parse it. Just structure the link like this, for example: https://jsonlint.com/?url=https://jsonlint.com/datasets/programming-languages.json...
🌐
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 ...
      » npm install valid-json-cli
    
Published   Apr 07, 2020
Version   1.4.1
Author   dotnetcarpenter