How can I pretty-print a JSON file from the command line?
Pipe the results from the file into the python json tool 2.6 onwards
python -m json.tool < 'file_name'
How can I minify JSON in a shell script?
TL;DR
no install
python -c 'import json, sys;json.dump(json.load(sys.stdin), sys.stdout)' < my.json
very fast (with jj)
jj -u < my.json
Perf benchmark
Here's the script, using hyperfine
:
#!/usr/bin/env bash
tmp=$(mktemp json.XXX)
tmp_md=$(mktemp md.XXX)
trap "rm $tmp $tmp_md" EXIT
cat <<JSON > $tmp
{
"foo": "lorem",
"bar": "ipsum"
}
JSON
hyperfine \
--export-markdown $tmp_md \
--warmup 100 \
"jj -u < $tmp" \
"yq eval -j -I=0 < $tmp" \
"xidel -s - -e '\$json' --printed-json-format=compact < $tmp" \
"jq --compact-output < $tmp" \
"python3 -c 'import json, sys;json.dump(json.load(sys.stdin), sys.stdout)' < $tmp" \
"ruby -r json -e 'j JSON.parse \$stdin.read' < $tmp"
pbcopy < $tmp_md
The result on my mac — MacBook Air (M1, 2020), 8 GB:
Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
---|---|---|---|---|
jj -u < json.p72 | 1.3 ± 0.2 | 0.9 | 2.7 | 1.00 |
yq eval -j -I=0 < json.p72 | 4.4 ± 0.4 | 3.8 | 7.8 | 3.37 ± 0.65 |
xidel -s - -e '$json' --printed-json-format=compact < json.p72 | 5.5 ± 0.3 | 5.0 | 6.5 | 4.19 ± 0.77 |
python3 -c 'import json, sys;json.dump(json.load(sys.stdin), sys.stdout)' < json.p72 | 14.0 ± 0.4 | 13.4 | 15.0 | 10.71 ± 1.89 |
jq --compact-output < json.p72 | 14.4 ± 2.0 | 13.2 | 33.6 | 11.02 ± 2.45 |
ruby -r json -e 'j JSON.parse $stdin.read' < json.p72 | 47.3 ± 0.6 | 46.1 | 48.5 | 36.10 ± 6.32 |
Converting JSON pretty print to one line
jq
or any other json
aware tool is best suited for json file manipulation.However here is awk
based solution.
awk -v RS= '{$1=$1}1' input.json
{ "endpointApplications": { "App_Name": { "connectionState": "Disconnected", "connectionTime": "No connection was established", "linkAttributes": { "ackSettings": { "dataAckEnabled": "true", "dataAckTimeout": "5000", "dataNakRetryLimit": "0", "retransmitDelay": "500" }, "keepAliveSettings": { "keepAliveAckTimeout": "5000", "keepAliveInterval": "30000" }, "logTraffic": "false", "port": "9999", "role": "server" }, "protocol": "snmp" } }, "queueStats": {} }
Note: This solution is mainly for the legacy systems not having tools like jq
and have no chance to get them installed due to some reasons.
What are good CLI tools for JSON?
I just found this:
http://stedolan.github.io/jq/
"jq is a lightweight and flexible command-line JSON processor."
2014 update:
@user456584 mentioned:
There's also the 'json' command (e.g. 'jsontool'). I tend to prefer it over jq. Very UNIX-y. Here's a link to the project: github.com/trentm/json –
in the json
README at http://github.com/trentm/json there is a long list of similar things
- jq: http://stedolan.github.io/jq/
- json:select: http://jsonselect.org/
- jsonpipe: https://github.com/dvxhouse/jsonpipe
- json-command: https://github.com/zpoley/json-command
- JSONPath: http://goessner.net/articles/JsonPath/, http://code.google.com/p/jsonpath/wiki/Javascript
- jsawk: https://github.com/micha/jsawk
- jshon: http://kmkeen.com/jshon/
- json2: https://github.com/vi/json2
- fx: https://github.com/antonmedv/fx
How to format JSON objects to be on individual lines in bash
As always when it comes to working with JSON in scripts and from the command line, jq
to the rescue:
$ jq -c . input.json
{"filename":"./readme.md","line":5,"rule":"MD009","aliases":["no-trailing-spaces"],"description":"Trailing spaces"}
{"filename":"./readme.md","line":6,"rule":"MD009","aliases":["no-trailing-spaces"],"description":"Trailing spaces"}
Related Topics
Bash Variable Assignment Not Working Expected
Force Unmount of Nfs-Mounted Directory
How to Use Nohup to Run Process as a Background Process in Linux
In Screen, How to Send a Command to All Virtual Terminal Windows Within a Single Screen Session
How to Find Lines Containing a String in Linux
Pass Parameter to an Awk Script File
Running Apt-Get for Another Partition/Directory
I Want to Remove Multiple Line of Text on Linux
How to Gzip All Files in All Sub-Directories into One Compressed File in Bash
Bash Shell Script - Check for a Flag and Grab Its Value
Internals of a Linux System Call
Trying to Use Bash on Windows and Got No Installed Distributions Message
Rm Fails to Delete Files by Wildcard from a Script, But Works from a Shell Prompt