How can I pretty-print JSON in a shell script?

Asked 2023-09-20 20:15:46 View 974,764

Is there a (Unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

{ "foo": "lorem", "bar": "ipsum" }

... into something like this:

{
    "foo": "lorem",
    "bar": "ipsum"
}

Answers

With Python 2.6+ you can do:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

For convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

For even more convenience with a bit more typing to get it ready:

prettyjson_s() {
    echo "$1" | python -m json.tool
}

prettyjson_f() {
    python -m json.tool "$1"
}

prettyjson_w() {
    curl "$1" | python -m json.tool
}

for all the above cases. You can put this in .bashrc and it will be available every time in shell. Invoke it like prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'.

Note that as @pnd pointed out in the comments below, in Python 3.5+ the JSON object is no longer sorted by default. To sort, add the --sort-keys flag to the end. I.e. ... | python -m json.tool --sort-keys.

Answered   2023-09-20 20:15:46

  • You could pipe that onto pygmentize -l javascript to get syntax colored output in your command line. Edit: If you have pygments installed that is. - anyone
  • A great answer, only caution I have with it is it does sort the keys on output - which you might need to be aware of. - anyone
  • In myy .vimrc "nnoremap <f5> :%!python -m json.tool<CR>:w<CR>" - anyone
  • This seems to escape Unicode characters into \uXXXX, which might be a disadvantage. - anyone
  • I've created an alias: alias pretty='python -mjson.tool | pygmentize -l json so that I can just run: command params | pretty. Hope this helps. PS: Should anyone manages to extend this to a) remove the curl-output I'm seeing every time and/or b) NOT sort the json keys; please do let me know, I will be highly thankful. - anyone

You can use: jq

It's very simple to use and it works great! It can handle very large JSON structures, including streams. You can find their tutorials here.

Usage examples:

$ jq --color-output . file1.json file1.json | less -R

$ command_with_json_output | jq .

$ jq # stdin/"interactive" mode, just enter some JSON

$ jq <<< '{ "foo": "lorem", "bar": "ipsum" }'
{
  "bar": "ipsum",
  "foo": "lorem"
}

Or use jq with identity filter:

$ jq '.foo' <<< '{ "foo": "lorem", "bar": "ipsum" }'
"lorem"

Answered   2023-09-20 20:15:46

  • There is also a --sort-keys option, which is helpful in some cases. - anyone
  • Working with curl: curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.' - anyone
  • "jq ." is great as a pretty-printer, but it comes with a caveat: jq (up to and including jq version 1.5) will alter the values of very large and very small numbers because it parses numeric values as IEEE 754 64-bit values. To check whether your favorite pretty-printer has the same issue, try this value: 1e1000. Note that python -mjson.tool fails this test badly in that it produces Infinity, which is not even JSON. - anyone
  • @Meekohi the alternative is "unnecessary use of echo". Super happy to have found the <<< operator – exactly what I was looking for. - anyone
  • jq is the best because it prints in colors! - anyone

I use the "space" argument of JSON.stringify to pretty-print JSON in JavaScript.

Examples:

// Indent with 4 spaces
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, 4);

// Indent with tabs
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, '\t');

From the Unix command-line with Node.js, specifying JSON on the command line:

$ node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));" \
  '{"foo":"lorem","bar":"ipsum"}'

Returns:

{
    "foo": "lorem",
    "bar": "ipsum"
}

From the Unix command-line with Node.js, specifying a filename that contains JSON, and using an indent of four spaces:

$ node -e "console.log(JSON.stringify(JSON.parse(require('fs') \
      .readFileSync(process.argv[1])), null, 4));"  filename.json

Using a pipe:

echo '{"foo": "lorem", "bar": "ipsum"}' | node -e \
"\
 s=process.openStdin();\
 d=[];\
 s.on('data',function(c){\
   d.push(c);\
 });\
 s.on('end',function(){\
   console.log(JSON.stringify(JSON.parse(d.join('')),null,2));\
 });\
"

Answered   2023-09-20 20:15:46

  • For debugging objects in Node.js, you should really use sys.inspect() instead of JSON.stringify(). Here's why: markhansen.co.nz/inspecting-with-json-stringify - anyone
  • Downvoted. The OP is about a "*nix command-line script" and this answer is a different context. - anyone
  • @danorton: JS can be used from the commandline via node.js and other similar solutions. - anyone
  • No need for the console: node -p "JSON.stringify(JSON.parse(process.argv[1]), null, '\t');" also outputs the result to STDOUT. - anyone
  • It sucks that the script is different for a filename vs. stdin - anyone

I wrote a tool that has one of the best "smart whitespace" formatters available. It produces more readable and less verbose output than most of the other options here.

underscore-cli

This is what "smart whitespace" looks like:

I may be a bit biased, but it's an awesome tool for printing and manipulating JSON data from the command-line. It's super-friendly to use and has extensive command-line help/documentation. It's a Swiss Army knife that I use for 1001 different small tasks that would be surprisingly annoying to do any other way.

Latest use-case: Chrome, Dev console, Network tab, export all as HAR file, "cat site.har | underscore select '.url' --outfmt text | grep mydomain"; now I have a chronologically ordered list of all URL fetches made during the loading of my company's site.

Pretty printing is easy:

underscore -i data.json print

Same thing:

cat data.json | underscore print

Same thing, more explicit:

cat data.json | underscore print --outfmt pretty

This tool is my current passion project, so if you have any feature requests, there is a good chance I'll address them.

Answered   2023-09-20 20:15:46

  • I also updated my ~/.bash_profile to have the following line: alias underscor='underscore print --outfmt pretty' now I can just do curl example.com/result.json | underscor and still use underscore for other formatting - anyone
  • Thanks Dave! Tool is good! alias pretty-json="underrsore pretty" and curl output pleasing an eye - anyone
  • Great formatting tool, just one note: forwarding output to a file (either with -o option or with > ) works only with underscore print. underscore pretty saves a file with color formatting signs being inserted, smth like: [32m, [33m, [39m along with some non-printable before each of them, which makes JSON not valid. However, underscore print alone doesn't add anything to a file and does its formatting job perfectly. - anyone
  • I love jq but this worked great for my "JSON" that didn't have double quoted keys. - anyone
  • @DaveDopson thanks for the great tool!! Gonna try using it alongside jsonselect.org/#tryit ... - anyone

I usually just do:

echo '{"test":1,"test2":2}' | python -mjson.tool

And to retrieve select data (in this case, "test"'s value):

echo '{"test":1,"test2":2}' | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["test"]'

If the JSON data is in a file:

python -mjson.tool filename.json

If you want to do it all in one go with curl on the command line using an authentication token:

curl -X GET -H "Authorization: Token wef4fwef54te4t5teerdfgghrtgdg53" http://testsite/api/ | python -mjson.tool

Answered   2023-09-20 20:15:46

  • if the json is supposed to come directly froma http api this is also a nice tool implemented in python: github.com/jkbr/httpie - anyone
  • If you have node installed (and don't mind the YAML style output) there's also this package: rafeca.com/prettyjson so you can end a curl with | prettyjson - anyone
  • As noted above, one of the problems with python -mjson.tool as a JSON pretty-printer is that it does not always emit JSON. E.g. 1e1000 becomes Infinity (whether using python 2.x or 3.x). 'jq .' always produces JSON, but it does not guarantee that very large (or very small values) are preserved exactly. - anyone

If you use npm and Node.js, you can do npm install -g json and then pipe the command through json. Do json -h to get all the options. It can also pull out specific fields and colorize the output with -i.

curl -s http://search.twitter.com/search.json?q=node.js | json

Answered   2023-09-20 20:15:46

It is not too simple with a native way with the jq tools.

For example:

cat xxx | jq .

Answered   2023-09-20 20:15:46

  • WARNING: jq encodes numbers as IEEE 754 64-bit floats, and thus their representation is likely to change. The precision of very small and very large numbers is likely to be lost. To check whether your favorite pretty-printer has the same issue, try this value: 1e1000. - anyone
  • or simply: jq . file.json ; but also cat file.json | jq (without the filter .) also works. (on ubuntu/linux; ymmv across platforms) - anyone
  • Already installed in Ubuntu and colors as well. Nice - anyone

Thanks to J.F. Sebastian's very helpful pointers, here's a slightly enhanced script I've come up with:

#!/usr/bin/python

"""
Convert JSON data to human-readable form.

Usage:
  prettyJSON.py inputFile [outputFile]
"""

import sys
import simplejson as json


def main(args):
    try:
        if args[1] == '-':
            inputFile = sys.stdin
        else:
            inputFile = open(args[1])
        input = json.load(inputFile)
        inputFile.close()
    except IndexError:
        usage()
        return False
    if len(args) < 3:
        print json.dumps(input, sort_keys = False, indent = 4)
    else:
        outputFile = open(args[2], "w")
        json.dump(input, outputFile, sort_keys = False, indent = 4)
        outputFile.close()
    return True


def usage():
    print __doc__


if __name__ == "__main__":
    sys.exit(not main(sys.argv))

Answered   2023-09-20 20:15:46

  • When the values are loaded into the dictionary, the order is lost: normal dict objects do not have a defined order. Try json.dumps(json.loads('{"b": 1, "a": 2}'), sort_keys=False) and you'll find they're swapped anyway. To fix it, import OrderedDict and load passing object_pairs_hook=OrderedDict. - anyone
  • You can change the script to read from standard input with this: inputFile = sys.stdin. This lets you pipe stuff to the script like so: curl http://somewhere.com/foo.json | pp_json.py - anyone
  • And to avoid sorting with @icktoofay's comment, import OrderedDict like this: from collections import OrderedDict. - anyone
  • Thanks @icktoofay. This allowed me to create the following vim function: com! FormatJSON %!python -c "from collections import OrderedDict; import sys; import json; j = json.load(sys.stdin, object_pairs_hook=OrderedDict); json.dump(j, sys.stdout, sort_keys=False, indent=4, separators=(',', ': '))" Note that the separators must be set as (',', ': ') to avoid trailing whitespace being added: bugs.python.org/issue16333 - anyone
  • Great snippet! I've used sort_keys = True instead, because I want to use this to compare json files and it works like a charm. - anyone

a simple bash script for pretty json printing

json_pretty.sh

#/bin/bash

grep -Eo '"[^"]*" *(: *([0-9]*|"[^"]*")[^{}\["]*|,)?|[^"\]\[\}\{]*|\{|\},?|\[|\],?|[0-9 ]*,?' | awk '{if ($0 ~ /^[}\]]/ ) offset-=4; printf "%*c%s\n", offset, " ", $0; if ($0 ~ /^[{\[]/) offset+=4}'

Example:

cat file.json | json_pretty.sh

Answered   2023-09-20 20:15:46

  • Thanks for the feedback. I just wrote this script today for personal using and it worked fine in my cases. I made fixes, now it's smaller and without this problem. There is not a goal to support completely format, but i can make other fixes if necessary. - anyone
  • That is only working answer I found. I have an embedded linux - no rubby, no javascript, no access to internet to download python modules... I have slightly different awk that does not support %*c notation so I changed the printf to c=0; while (c++<offset) printf " "; printf $0;. And my awk has different regex escaping and backslash does not work in []. I changed the regexes to /^[[{]/ and /[]}]/. - anyone
  • This should be the accepted, as it's full native and don't require third party software... - anyone
  • @EvgenyKarpov nice catch, no need for a full rollback, just the grep that was erased by mistake ;) - anyone
  • This script is pretty fragile and it doesn't support full json syntax. For example it turns { "\"" : "quote" } into { "\" " : " } (on multiple lines). - anyone

With Perl, use the CPAN module JSON::XS. It installs a command line tool json_xs.

Validate:

json_xs -t null < myfile.json

Prettify the JSON file src.json to pretty.json:

< src.json json_xs > pretty.json

If you don't have json_xs, try json_pp . "pp" is for "pure perl" – the tool is implemented in Perl only, without a binding to an external C library (which is what XS stands for, Perl's "Extension System").

Answered   2023-09-20 20:15:46

  • Seems to come standard with Cygwin! - anyone
  • json_pp can be used in the same way and is most probably readily installed on your system (on Debian it is in the 'perl' package). - anyone
  • FYI, on my Mac OS X 10.9 system, json_pp is available automatically. - anyone
  • -t null gave me null: not a valid toformat... but leaving it off worked splendidly. Thanks. - anyone
  • pp is for pure perl, not pretty print here :) Since json_xs and json_pp can do more than just pretty print and also with -json_opt do other things instead of pretty printing. Although pretty printing is the default behaviour. - anyone

On *nix, reading from stdin and writing to stdout works better:

#!/usr/bin/env python
"""
Convert JSON data to human-readable form.

(Reads from stdin and writes to stdout)
"""

import sys
try:
    import simplejson as json
except:
    import json

print json.dumps(json.loads(sys.stdin.read()), indent=4)
sys.exit(0)

Put this in a file (I named mine "prettyJSON" after AnC's answer) in your PATH and chmod +x it, and you're good to go.

Answered   2023-09-20 20:15:46

  • Indeed, using stdin/stdout is much more flexible and simple. Thanks for pointing it out. - anyone
  • For programs that expect a named file, use /dev/stdin, ditto for out and err. - anyone
  • FYI fileinput.input() reads from stdin if there are no files given at a command-line. Example - anyone
  • fileinput.input() can't deal well with files with no newline at the end, last time I checked. - anyone
  • he askes in shell script, not python other language. With JQ can do it perfectly. - anyone

That's how I do it:

curl yourUri | json_pp

It shortens the code and gets the job done.

Answered   2023-09-20 20:15:46

  • Ubuntu server: If you have production machines with very restricted installations, this might be the best choice as it is installed by default under a specific name. Python is often installed in different ways (eg python3, python or not at all) depending on the version. - anyone
  • works for mac :) - anyone
  • Works for both MacOS and Linux (Debian) by default. I'd recommend this answer - anyone

The JSON Ruby Gem is bundled with a shell script to prettify JSON:

sudo gem install json
echo '{ "foo": "bar" }' | prettify_json.rb

Script download: gist.github.com/3738968

Answered   2023-09-20 20:15:46

  • Note that this solution decode the unicode "\uxxxx" escape sequences, unlike the Python one with json.tool. However, it also seems to have nesting depth limitations (nesting of 20 is too deep (JSON::NestingError)). - anyone
  • on Ubuntu you can do: sudo apt-get install ruby-json-pure instead of gem install - anyone
  • ```eric-mbp:~ ericstob$ sudo gem install json Password: Fetching: json-1.7.3.gem (100%) Building native extensions. This could take a while... Successfully installed json-1.7.3 1 gem installed Installing ri documentation for json-1.7.3... Installing RDoc documentation for json-1.7.3... eric-mbp:~ ericstob$ prettify_json.rb -bash: prettify_json.rb: command not found - anyone
  • maybe you could post the contents of your prettify_json.rb? - anyone
  • You can download the script, move it to your ~/bin folder (make sure it's in your PATH) rename prettify_json.rb to ppj and run chmod +x ppj. Now you can do something like curl www.jsonsring.com/something.json | ppj - anyone
$ echo '{ "foo": "lorem", "bar": "ipsum" }' \
> | python -c'import fileinput, json;
> print(json.dumps(json.loads("".join(fileinput.input())),
>                  sort_keys=True, indent=4))'
{
    "bar": "ipsum",
    "foo": "lorem"
}

NOTE: It is not the way to do it.

The same in Perl:

$ cat json.txt \
> | perl -0007 -MJSON -nE'say to_json(from_json($_, {allow_nonref=>1}), 
>                                     {pretty=>1})'
{
   "bar" : "ipsum",
   "foo" : "lorem"
}

Note 2: If you run

echo '{ "Düsseldorf": "lorem", "bar": "ipsum" }' \
| python -c'import fileinput, json;
print(json.dumps(json.loads("".join(fileinput.input())),
                 sort_keys=True, indent=4))'

the nicely readable word becomes \u encoded

{
    "D\u00fcsseldorf": "lorem", 
    "bar": "ipsum"
}

If the remainder of your pipeline will gracefully handle unicode and you'd like your JSON to also be human-friendly, simply use ensure_ascii=False

echo '{ "Düsseldorf": "lorem", "bar": "ipsum" }' \
| python -c'import fileinput, json;
print json.dumps(json.loads("".join(fileinput.input())),
                 sort_keys=True, indent=4, ensure_ascii=False)'

and you'll get:

{
    "Düsseldorf": "lorem", 
    "bar": "ipsum"
}

Answered   2023-09-20 20:15:46

  • actually I do the same but with javascript itself :) - anyone
  • In the version of the JSON module I have, to_json doesn't seem to accept options. But this works: perl -MJSON -nE 'say JSON->new->pretty->encode(from_json $_)' text.json - anyone
  • The Python example could be simplified. It's much easier to pipe JSON output straight into python -m json.tool. - anyone
  • @Dan: yes. And there are several answers that show json.tool code examples. 1. this version allows you to change some parameters e.g., indent 2. At the time of the posting (2008) Python 2.4 was still used that doesn't support -mjson.tool - anyone
  • @dwlz command-line version with unicode characters (requires python3.9+): python -m json.tool --no-ensure-ascii in.json out.json - anyone

UPDATE I'm using jq now as suggested in another answer. It's extremely powerful at filtering JSON, but, at its most basic, also an awesome way to pretty print JSON for viewing.

jsonpp is a very nice command line JSON pretty printer.

From the README:

Pretty print web service responses like so:

curl -s -L http://<!---->t.co/tYTq5Pu | jsonpp

and make beautiful the files running around on your disk:

jsonpp data/long_malformed.json

If you're on Mac OS X, you can brew install jsonpp. If not, you can simply copy the binary to somewhere in your $PATH.

Answered   2023-09-20 20:15:46

  • I tried jsonpp (used in the past successful) against a huge file (>60MB). I stopped it after 5min. I piped it into python -mjson.tool (from other answer here) and it took 10-20sec... - anyone
  • 60MB of JSON? Wow! I don't typically deal with files that big but useful to know. Thanks. - anyone
  • On my ubuntu box I have a json_pp - which does format json nicely, although despite the similarity in naming, I believe this to be an entirely different project from the jsonpp mentioned here - anyone

Try pjson. It has colors!

echo '{"json":"obj"} | pjson

Install it with pip:

⚡ pip install pjson

And then pipe any JSON content to pjson.

Answered   2023-09-20 20:15:46

  • It requires python-pip (sudo apt-get install python-pip) and then (sudo pip install pjson) The great advantage are colours! - anyone
  • The disadvantage is it is not possible to grep coloured output. - anyone

Or, with Ruby:

echo '{ "foo": "lorem", "bar": "ipsum" }' | ruby -r json -e 'jj JSON.parse gets'

Answered   2023-09-20 20:15:46

  • That gives me an error. Do you need some ruby json package installed? - anyone
  • Yes, you need the JSON Ruby Gem: sudo gem install json - anyone
  • @MatSchaffer Note that this does not work if you are using JSON to serialize objects with custom to_json methods; Kernel#jj only pretty-prints arrays and hashes of the same (or numbers/strings/booleans). - anyone
  • On Windows, try this: echo { "foo": "lorem", "bar": "ipsum" } | ruby -r json -e 'jj JSON.parse gets' - anyone

You can use this simple command to achieve the result:

echo "{ \"foo\": \"lorem\", \"bar\": \"ipsum\" }"|python -m json.tool

Answered   2023-09-20 20:15:46

I use jshon to do exactly what you're describing. Just run:

echo $COMPACTED_JSON_TEXT | jshon

You can also pass arguments to transform the JSON data.

Answered   2023-09-20 20:15:46

  • Thanks, jshon is a lot faster than using python or ruby for the same task - anyone
  • @Alexander - How fast a pretty printer do you need? I'm on OSx Lion that comes with Python preinstalled. With python -mjson.tool I can pretty print a 96KB json file in 0.1s - the json output of earthporn that jshon links to is about 24KB and I can pretty print that in 0.08s. How much faster is jshon for you? - anyone
  • I'm working with 1+GB compressed (who even knows how big uncompressed) JSON data files, so I very much appreciate the suggestion that jshon is faster. - anyone

Check out Jazor. It's a simple command line JSON parser written in Ruby.

gem install jazor
jazor --help

Answered   2023-09-20 20:15:46

  • Is it just me or is this the only suggestion that actually answers the OP's question? I came here looking for a simple command into which I could pipe the output of curl and this is the only one that did it for me. - anyone
  • I like that it has the option to colorize the output. Makes it easier to read. - anyone
  • ooh I also like the option to pass a url since I am using this to view the output of my REST API - anyone

You only need to use jq

If jq is not installed then you need to install jq first:

sudo apt-get update
sudo apt-get install jq

After installing jq then only need to use jq:

echo '{ "foo": "lorem", "bar": "ipsum" }' | jq

Output looks like

{
  "foo": "lorem",
  "bar": "ipsum"
}

Answered   2023-09-20 20:15:46

  • Or brew install jq if you're on a mac. - anyone

JSONLint has an open-source implementation on GitHub that can be used on the command line or included in a Node.js project.

npm install jsonlint -g

and then

jsonlint -p myfile.json

or

curl -s "http://api.twitter.com/1/users/show/user.json" | jsonlint | less

Answered   2023-09-20 20:15:46

  • I recommend not installing nodejs/npm dependencies globally => I'd use npx instead: curl -s "http://api.twitter.com/1/users/show/user.json" | npx jsonlint | less (i.e. no npm install necessary) - anyone

Simply pipe the output to jq ..

Example:

twurl -H ads-api.twitter.com '.......' | jq .

Answered   2023-09-20 20:15:46

  • Nice answer @Ackshaey Singh and one can re-direct the same to a file easily as well. e.g. cat <file_name.txt> | jq . > <output_name.txt> - anyone
  • brew install jq if your are on mac os. - anyone
  • Unfortunately, using jq . for pretty-printing has one potential drawback: all extant versions of jq insist on treating JSON numbers as IEEE numbers, so precision is easily lost, e.g. for very large integers. - anyone
  • @Pramit cat file | is invariably a waste of a process; just do jq . <file_name.txt >output_name.txt (with literal < and > characters). - anyone

You can simply use standard tools like jq or json_pp.

echo '{ "foo": "lorem", "bar": "ipsum" }' | json_pp

or

echo '{ "foo": "lorem", "bar": "ipsum" }' | jq

will both prettify output like the following (jq even more colorful):

{
  "foo": "lorem",
  "bar": "ipsum"
}

The huge advantage of jq is that it can do A LOT more if you'd like to parse and process the json.

Answered   2023-09-20 20:15:46

With Perl, if you install JSON::PP from CPAN you'll get the json_pp command. Stealing the example from B Bycroft you get:

[pdurbin@beamish ~]$ echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp
{
   "bar" : "ipsum",
   "foo" : "lorem"
}

It's worth mentioning that json_pp comes pre-installed with Ubuntu 12.04 (at least) and Debian in /usr/bin/json_pp

Answered   2023-09-20 20:15:46

Pygmentize

I combine Python's json.tool with pygmentize:

echo '{"foo": "bar"}' | python -m json.tool | pygmentize -g

There are some alternatives to pygmentize which are listed in my this answer.

Here is a live demo:

Demo

Answered   2023-09-20 20:15:46

  • Sometimes one needs to use pygmentize -l json to get colourfication. - anyone
  • Install with apt package python-pygments, i.e. apt-get install python-pygments - anyone

I recommend using the json_xs command line utility which is included in the JSON::XS perl module. JSON::XS is a Perl module for serializing/deserializing JSON, on a Debian or Ubuntu machine you can install it like this:

sudo apt-get install libjson-xs-perl

It is obviously also available on CPAN.

To use it to format JSON obtained from a URL you can use curl or wget like this:

$ curl -s http://page.that.serves.json.com/json/ | json_xs

or this:

$ wget -q -O - http://page.that.serves.json.com/json/ | json_xs

and to format JSON contained in a file you can do this:

$ json_xs < file-full-of.json

To reformat as YAML, which some people consider to be more humanly-readable than JSON:

$ json_xs -t yaml < file-full-of.json

Answered   2023-09-20 20:15:46

jj is super-fast, can handle ginormous JSON documents economically, does not mess with valid JSON numbers, and is easy to use, e.g.

jj -p # for reading from STDIN

or

jj -p -i input.json

It is (2018) still quite new so maybe it won’t handle invalid JSON the way you expect, but it is easy to install on major platforms.

Answered   2023-09-20 20:15:46

bat is a cat clone with syntax highlighting:

Example:

echo '{"bignum":1e1000}' | bat -p -l json

-p will output without headers, and -l will explicitly specify the language.

It has colouring and formatting for JSON and does not have the problems noted in this comment: How can I pretty-print JSON in a shell script?

Answered   2023-09-20 20:15:46

Install yajl-tools with the command below:

sudo apt-get install yajl-tools

then,

echo '{"foo": "lorem", "bar": "ipsum"}' | json_reformat

Answered   2023-09-20 20:15:46

  • Awesome. Does not require another language/interpreter and is in the package repo's, no need to brew! - anyone