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"
}
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"
}
NOTE: It is not the way to do it.
The same in Perl:
Note 2: If you run
the nicely readable word becomes \u encoded
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
and you'll get:
jj is super-fast, can handle ginormous JSON documents economically, does not mess with valid JSON numbers, and is easy to use, e.g.
or
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.
Thanks to J.F. Sebastian's very helpful pointers, here's a slightly enhanced script I've come up with:
Or, with Ruby:
I use the "space" argument of
JSON.stringify
to pretty-print JSON in JavaScript.Examples:
From the Unix command-line with nodejs, specifying json on the command line:
Returns:
From the Unix command-line with Node.js, specifying a filename that contains JSON, and using an indent of four spaces:
Using a pipe:
On *nix, reading from stdin and writing to stdout works better:
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.