JSON Command Line Formatter Tool for Linux

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:
























































CommandMean [ms]Min [ms]Max [ms]Relative
jj -u < json.p721.3 ± 0.20.92.71.00
yq eval -j -I=0 < json.p724.4 ± 0.43.87.83.37 ± 0.65
xidel -s - -e '$json' --printed-json-format=compact < json.p725.5 ± 0.35.06.54.19 ± 0.77
python3 -c 'import json, sys;json.dump(json.load(sys.stdin), sys.stdout)' < json.p7214.0 ± 0.413.415.010.71 ± 1.89
jq --compact-output < json.p7214.4 ± 2.013.233.611.02 ± 2.45
ruby -r json -e 'j JSON.parse $stdin.read' < json.p7247.3 ± 0.646.148.536.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



Leave a reply



Submit