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.
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.
The very simplest way is using json_pp (json pretty print) from perl.
$ echo {} | json_pp
{}
$ echo [] | json_pp
[]
$ echo 5 | json_pp
5
$ echo a | json_pp
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "a\n") at /usr/bin/json_pp line 59.
For file you can do
$ cat file.json | json_pp
For more info
$ man json_pp
You can use jq and send stdout to null
sudo apt-get install jq
jq . /path/to/yourfile.json 1> /dev/null
Example Execution
Here's some example runs
No Errors
If there are no errors in the json file, then there will be no output and the exit code is 0
user@host:~$ jq . updates/v1/meta.json 1> /dev/null
user@host:~$ echo $?
0
user@host:~$
Errors
If there are errors in the json file, then the output will tell you exactly which line & character, and the exit code will be non-zero
user@host:~$ jq . updates/v1/meta.json 1> /dev/null
parse error: Expected another array element at line 15, column 8
user@host:~$ echo $?
4
user@host:~$
sudo apt install jsonlint
and then do jsonlint-php {file}
Manual page:
OPTIONS
-q, --quiet
Cause jsonlint to be quiet when no errors are found
-h, --help
Show this message
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":.
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
sedonly 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.
As of version 2.4, jsonschema includes a command-line program to check some input json file against a schema specified in another file.
You can call it like so:
jsonschema -i B.json BSchema.json
Here's some example output from when I put an object where an array should be:
{u'description': u'Doubles resource collection.'}: {u'description': u'Doubles resource collection.'} is not of type u'array'
In Powershell Core there is a built-in method that allows you to do the validation test-json. PowerShell syntax: Test-Json [-Json] <String> [[-Schema] <String>]. For more information have a look here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/test-json
In the question it was also asked about the environments. Powershell Core v.7.0+ is cross-platform so this validation can be done anywhere. If you can't or don't want to use PowerShell you can try some Python tool like this: Validate JSON file syntax in shell script without installing any package
» npm install valid-json-cli