Last Updated: July 12, 2022
·
56.86K
· originell

Commandline JSON Pretty Print everywhere

You can pretty print (and validate) json on nearly every *nix-like (unix, linux, osx,…) system, since most of them have Python (2.6+ required) installed.

echo '{"test1": 1, "test2": "win"}' | python -m json.tool

will be returned as:

{
    "test1": 1, 
    "test2": "win"
}

And yep, you guessed it, this can easily be integrated with your favorite editor of choice, as long as it supports passing data to a shell command.

Pretty printing a JSON file with VIM and Python:

:%!python -m json.tool

Related protips:

Basic Vim commands - For getting started

12 Responses
Add your response

If the machine has pygments installed (not as likely, but easy to install) you can pipe the json for color output as well python -mjson.tool | pygmentize -l javascript.

over 1 year ago ·

{"comment": "Pretty Helpful"}

over 1 year ago ·

Here's a NodeJS version:

echo '{"foo":"bar"}' | json

You need to install the jsontool module (npm install -g jsontool)

over 1 year ago ·

You insipred me to attempt to repeat this in NodeJS without 3rd party modules. Turned out it's shorter in Python due to json.tool moduel existence :)

Pretty print JSON from command line with NodeJS

over 1 year ago ·

Ruby version: ruby -rjson -e 'puts JSON.pretty_generate(JSON.load($<))'

over 1 year ago ·

@dpashkevich Hey! Great job :) Pretty short for not using any third party module!

@manveru ah that's a nice one too :)

over 1 year ago ·

I usually use this little wrapper: https://github.com/igorgue/pjson which adds pretty colors.

over 1 year ago ·

I wish I could get the pretty color version when viewing JSON files in ST2. You'd think it was possible seeing as ST2 is heavily connected to Python

over 1 year ago ·

I use this neat little tool: www.jsonprettyprint.net. Does the job for me.

over 1 year ago ·

Very helpful, specifically the VIM shortcut. I've reblogged this post along with additional JSON pretty printing tools at http://techzog.com/development/json-and-xml-in-human-readable-form/

over 1 year ago ·

I like Stephen Dolan's jq for working with JSON on the CLI: http://stedolan.github.io/jq

over 1 year ago ·

+1 for jq:

$ echo '{"test1": 1, "test2": "win"}' | jq .

The output is even colored.

over 1 year ago ·