JavaScript Object Notation.
For JSON basics, read Wikipedia.
Useful JSON tools:
- gron
- jq
- jnv
- jgrep
- Java Libraries
- Conversion
- Pretty Print / format JSON
- JSON Linting
- JSON Comments
- Memes
Flattens JSON to be greppable.
gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" |
fgrep "commit.author"unflatten back to expanded JSON:
gron "https://api.github.com/repos/tomnomnom/gron/commits?per_page=1" |
fgrep "commit.author" |
gron -uJSON Query filters json inputs - file or stdin.
Pre-compiled C available for all platforms.
Widely used in my scripts in DevOps-Bash-tools repo.
https://jqlang.github.io/jq/tutorial/
https://jqlang.github.io/jq/manual/
jq returns literal null string for fields that don't exist, this is annoying af in bash scripts where you will
often be testing for failing to find something using [ -z "$output" ].
To avoid this and have jq return a default value, blank in this case, instead of a literal null:
jq -r '.non_existent_key // ""' < file.jsonFind and output the policy field from anywhere in the JSON.
Most of the time you should understand the schema and specify it explicitly.
jq -r '.. | objects | select(has("policy")) | .policy'..recursesobjectsfilters to only objects (ignores arrays & scalars)select(has("policy"))returns only objects with apolicyfield.policyselects that field out of those subset of objects
Interactive JSON viewer.
Useful to quickly create jq queries by drilling down interactively.
cat data.json | jnvor
jnv data.jsonTab completion.
Ctrl-q - copy jq query to clipboard to paste to code.
Ctrl-o - copy the JSON to clipboard.
JSON grep.
xml2jsonyq -P < "$file"or from DevOps-Bash-tools:
json2yaml.sh "$file"or
json2yaml.sh < "$file"This will figure out which of the following you have installed and use the first one it finds in this order:
ruby -r yaml -r json -e 'puts YAML.dump(JSON.parse(STDIN.read))' < "$file"python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < "$file"This script doesn't use these any more:
yq- there were two different ones found in$PATHthat could result in different results:- Perl
JSON::XS- sorted the keys, losing the original structure of the file where the variables were at the top for human readability
- found when converting a json to yaml that it converted this:
"ssh_pty": trueto this useless thing:
ssh_pty: !!perl/scalar:JSON::PP::Boolean 1json2csvPerl JSON::XS CPAN module (doesn't sort keys):
json_xsPython 2.6+ (sorts keys):
python -m json.toolFrom DevOps-Bash-tools, recursively find and lint all *.json files:
check_json.shFrom DevOps-Python-tools, recursively find and lint all *.json files:
validate_json.py .I run these automatically in all my GitHub repos via CI/CD.
JSON does not allow comments like # or // in programming languages.
If you try to add them it will break linting and real-world parsing of the JSON.
JSON comment support was removed intentionally because some bad programmers were using them to contain things like parsing directories, breaking parsing compatibility between systems.
As a workaround, you can create key pairs with unused keys like _comment:
{
"_comment": "This is a commment about why this JSON content sucks",
"name": "Hari Sekhon",
"_comment2": "Another comment must have a unique key"
}
