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" } Found 4 already available tools in my Gentoo system:
From package dev-libs/json-glib 19K ELF
json-glib-format -p file.json From package dev-lang/perl 4,9K Perl script
(keeps unicode symbols as-is)
cat file.json | json_pp From package dev-libs/yajl 43K ELF
cat file.json | json_reformat From package dev-lang/python
(escapes unicode symbols to unreadable \u hex notation, had to replace python with python3 on debian system)
python -m json.tool file.json You can also use online tools instead if that is an option for you.
I find http://jsonprettyprint.net to be the simplest and easiest.
I know that the original post asked for a shell script, but there are so many useful and irrelevant answers that probably did not help the original author. Adding on to irrelevance :)
BTW I could not get any command line tools to work.
If somebody want simple JSON JavaScript code, they could do:
JSON.stringfy(JSON.parse(str), null, 4) http://www.geospaces.org/geoweb/Wiki.jsp?page=JSON%20Utilities%20Demos
Here is JavaScript code that not only pretties the JSON, but orders them by their attribute or by attribute and level.
If input is
{ "c": 1, "a": {"b1": 2, "a1":1 }, "b": 1}, it either prints (groups all the objects together):
{ "b": 1, "c": 1, "a": { "a1": 1, "b1": 2 } } OR (just orders by key):
{ "a": { "a1": 1, "b1": 2 }, "b": 1, "c": 1 }
jsonlibrary, but I added pygments as well to get syntax highlighting.