79

Is there an editor or tool for Linux command line to format JSON data?

Peter Mortensen
31.5k22 gold badges110 silver badges134 bronze badges
asked Mar 9, 2011 at 9:43
3

4 Answers 4

108
 alias pp='python -mjson.tool'
 pp mydata.json

From the first link in the accepted answer: http://ruslanspivak.com/2010/10/12/pretty-print-json-from-the-command-line/

Modern distros may require python3 instead of python.

mwfearnley
3,8162 gold badges39 silver badges39 bronze badges
answered Apr 17, 2012 at 9:09

3 Comments

Outdated answer. Use jq.
not work for bool true and false, only work for True in python format.
This answer is not outdated, it continues to work with python3 -m json.tool. However, jq will give you pretty colors :)
89

jq is a lightweight and flexible command-line JSON processor.

http://stedolan.github.io/jq/

jq is like sed for JSON data – you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

jq is written in portable C, and it has zero runtime dependencies. You can download a single binary, scp it to a far away machine, and expect it to work.

answered May 12, 2013 at 23:50

3 Comments

To me this solution seems the best: easy to install, and output keeps the order of attributes from input - this might be sometime very practical when authoring documentation as it supports better readability. Typical use: $ jq . data.json (mind the dot after the jq).
Indeed jq is superb. Once you have jq installed, in Vim you can paste any JSON into a new buffer and run :%!jq '.' this pretty prints the JSON in the buffer. Source: Vim: prettify JSON
Don't use jq . data.json > data.json the redirection could truncate the file before jq reads it. Using pipes with tee has the same repercussions. askubuntu.com/questions/752174/… Instead, either create a temporary file, or use a tool like sponge.
20

On Ubuntu jsonlint is provided by apt:python3-demjson

Usage:

$ sudo apt install -y python3-demjson
$ jsonlint -f input.json > output.json
answered May 27, 2012 at 21:36

3 Comments

Great for when the JSON you're looking at doesn't conform to the spec. Other tools (jq, the Python json module) require completely conformant JSON for them to work.
It appears that in current packages the command installed with the "python-demjson" package is "jsonlint-py" instead of "jsonlint".
The apt package appears to be called python3-demjson by now. The command installed with this package is still called "jsonlint" however.
9

Add to vimrc:

" Format JSON data
map <C-F6> :%!python -m json.tool<CR>

And you can use the shortcut CTRL+F6 to format json data


Or just under vim's command mode:

%!python -m json.tool
answered Feb 24, 2013 at 3:18

1 Comment

If you're using vim you can: au FileType json set equalprg=python\ -m\ json.tool and format with =.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.