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.
🌐
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%
🌐
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 ...
🌐
GitHub
github.com › wiztools › json-validator
GitHub - wiztools/json-validator: Cli Json validator and formatter · GitHub
Command line tool to validate and format JSON files. Mac users can use homebrew to install json-validator:
Author   wiztools
🌐
#!/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 416 users
Languages   JavaScript 91.2% | HTML 3.8% | Yacc 3.0% | Lex 1.1% | Makefile 0.9%
Find elsewhere
🌐
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.
🌐
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 ...
🌐
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 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.
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 › 5074 › validate-json
validate json
March 16, 2010 - local alternative: echo '{"json":"obj"}' | python -mjson.tool as metioned in: https://www.commandlinefu.com/commands/view/9652/using-json.tool-from-the-shell-to-validate-and-pretty-print https://www.commandlinefu.com/commands/view/2144/validate-and-pretty-print-json-expressions. also, 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/
🌐
GitHub
github.com › ajv-validator › ajv-cli
GitHub - ajv-validator/ajv-cli: Command-line interface for Ajv JSON Validator · GitHub
ajv compile -s "schema.json" -o "validate_schema.js" -o without parameter should be used to output code to stdout to pass it to some code formatter. This command also supports parameters -r, -m and -c as in validate command.
Starred by 304 users
Forked by 75 users
Languages   TypeScript 96.5% | JavaScript 3.5%
🌐
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
🌐
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
🌐
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.