How to beautify JSON in Python?

PythonJsonCommand Line

Python Problem Overview


Can someone suggest how I can beautify JSON in Python or through the command line?

The only online based JSON beautifier which could do it was: <http://jsonviewer.stack.hu/>;.

I need to use it from within Python, however.

This is my dataset:

{ "head": {"vars": [ "address" , "description" ,"listprice" ]} , "results": { "bindings": [ 
	{
		"address" : { "type":"string", "value" : " Dyne Road, London NW6"},
			"description" :{ "type":"string", "value" : "6 bed semi detached house"},
			"listprice" : { "type":"string", "value" : "1,150,000"}
	}
	,
		{
			"address" : { "type":"string", "value" : " Tweedy Road, Bromley BR1"},
			"description" :{ "type":"string", "value" : "5 bed terraced house"},
			"listprice" : { "type":"string", "value" : "550,000"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Vera Avenue, London N21"},
			"description" :{ "type":"string", "value" : "4 bed detached house"},
			"listprice" : { "type":"string", "value" : "

				995,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Wimbledon Park Side, London SW19"},
			"description" :{ "type":"string", "value" : "3 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Westbere Road, West Hampstead, London NW2"},
			"description" :{ "type":"string", "value" : "5 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " The Avenue, Hatch End, Pinner HA5"},
			"description" :{ "type":"string", "value" : "5 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Princes Park Avenue, London NW11"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Canons Drive, Edgware HA8"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Westbere Road, West Hampstead NW2"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Haymills Estate, Ealing, London"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"},
			"description" :{ "type":"string", "value" : "5 bedroom  terraced house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"},
			"description" :{ "type":"string", "value" : "5 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Northwick Close, St John's Wood NW8"},
			"description" :{ "type":"string", "value" : "3 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Claremont Gardens, Surbiton KT6"},
			"description" :{ "type":"string", "value" : "13 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Dene Terrace Woodclyffe Drive, Chislehurst, Kent BR7"},
			"description" :{ "type":"string", "value" : "5 bedroom  end terrace house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Stamford Road, London N1"},
			"description" :{ "type":"string", "value" : "4 bedroom  terraced house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Stanhope Avenue, London N3"},
			"description" :{ "type":"string", "value" : "6 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Haymills Estate, Ealing, London"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Elms Crescent, London SW4"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Princes Park Avenue, London NW11"},
			"description" :{ "type":"string", "value" : "4 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Abbeville Road, London SW4"},
			"description" :{ "type":"string", "value" : "4 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Canons Drive, Edgware HA8"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Henson Avenue, Willesdon Green NW2"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Woodstock Road, London NW11"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Tamworth Street, London SW6"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Stanhope Avenue, Finchley, London"},
			"description" :{ "type":"string", "value" : "5 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " The Old Burlington, Church Street, London W4"},
			"description" :{ "type":"string", "value" : "3 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Ebury Close, Northwood HA6"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Middleton Road, London NW11"},
			"description" :{ "type":"string", "value" : "4 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Henson Avenue, Willesden Green NW2"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Huron Road, London SW17"},
			"description" :{ "type":"string", "value" : "6 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Corringway, Ealing W5"},
			"description" :{ "type":"string", "value" : "5 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Woodlands Avenue, New Malden KT3"},
			"description" :{ "type":"string", "value" : "5 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Gunnersbury Park Area, Ealing, London"},
			"description" :{ "type":"string", "value" : "6 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Blenheim Gardens, London, Brent NW2"},
			"description" :{ "type":"string", "value" : "6 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Creighton Road, London NW6"},
			"description" :{ "type":"string", "value" : "4 bedroom  terraced house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Plaistow Lane, Bromley BR1"},
			"description" :{ "type":"string", "value" : "7 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Greenfield Gardens, London NW2"},
			"description" :{ "type":"string", "value" : "4 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Hendon Avenue, London N3"},
			"description" :{ "type":"string", "value" : "3 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Peckham Park Road, London SE15"},
			"description" :{ "type":"string", "value" : "6 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Woodclyffe Drive, Chislehurst BR7"},
			"description" :{ "type":"string", "value" : "5 bedroom  house for sale"},
			"listprice" : { "type":"string", "value" : "

				From 1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Highwood Hill, Mill Hill, London"},
			"description" :{ "type":"string", "value" : "5 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Stanhope Avenue, London N3"},
			"description" :{ "type":"string", "value" : "5 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Kersley Mews, London SW11"},
			"description" :{ "type":"string", "value" : "3 bedroom  mews for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Ebury Close, Northwood HA6"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Ellesmere Road, Chiswick W4"},
			"description" :{ "type":"string", "value" : "6 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " The Avenue, Hatch End, Pinner, Middlesex"},
			"description" :{ "type":"string", "value" : "5 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Wandsworth, London SW18"},
			"description" :{ "type":"string", "value" : "6 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Carlton Road, New Malden KT3"},
			"description" :{ "type":"string", "value" : "4 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " St Mary's Mews, Ealing W5"},
			"description" :{ "type":"string", "value" : "3 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Ritherdon Road, Balham, London SW17"},
			"description" :{ "type":"string", "value" : "5 bedroom  semi detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Goldsmith Avenue, London W3"},
			"description" :{ "type":"string", "value" : "5 bedroom  property for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	,
		{
			"address" : { "type":"string", "value" : " Plaistow Lane, Bromley, Kent BR1"},
			"description" :{ "type":"string", "value" : "7 bedroom  detached house for sale"},
			"listprice" : { "type":"string", "value" : "

				1,250,000


					"}
		}
	] } }

Python Solutions


Solution 1 - Python

From the command-line:

echo '{"one":1,"two":2}' | python -mjson.tool

which outputs:

{
    "one": 1, 
    "two": 2
}

Programmtically, the Python manual describes pretty-printing JSON:

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
    "4": 5,
    "6": 7
}

Solution 2 - Python

Use the indent argument of the dumps function in the json module.

From the docs:

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4)
{
    "4": 5,
    "6": 7
}

Solution 3 - Python

A minimal in-python solution that colors json data supplied via the command line:

import sys
import json
from pygments import highlight, lexers, formatters

formatted_json = json.dumps(json.loads(sys.argv[1]), indent=4)
colorful_json = highlight(unicode(formatted_json, 'UTF-8'), lexers.JsonLexer(), formatters.TerminalFormatter())
print(colorful_json)

Inspired by pjson mentioned above. This code needs pygments to be installed.

Output example:

enter image description here

Solution 4 - Python

Try underscore-cli:

cat myfile.json | underscore print --color

https://raw.github.com/ddopson/underscore-cli/master/doc/example.png"></img>

It's a pretty nifty tool that can elegantly do a lot of manipulation of structured data, execute js snippets, fill templates, etc. It's ridiculously well documented, polished, and ready for serious use. And I wrote it. :)

Solution 5 - Python

I didn't like the output of json.dumps(...) -> For my taste way too much newlines. And I didn't want to use a command line tool or install something. I finally found Pythons pprint (= pretty print). Unfortunately it doesn't generate proper JSON but I think it is useful to have a user friendly glympse at the stored data.

Output of json.dumps(json_dict, indent=4)

{
    "hyperspace": {
        "constraints": [],
        "design": [
            [
                "windFarm.windparkSize.k",
                "continuous",
                [
                    0,
                    0,
                    5
                ]
            ],
            [
                "hydroPlant.primaryControlMax",
                "continuous",
                [
                    100,
                    300
                ]
            ]
        ],
        "kpis": [
            "frequency.y",
            "city.load.p[2]"
        ]
    },
    "lhc_size": 10,
    "number_of_runs": 10
}

Usage of pprint:

import pprint

json_dict = {"hyperspace": {"constraints": [], "design": [["windFarm.windparkSize.k", "continuous", [0, 0, 5]], ["hydroPlant.primaryControlMax", "continuous", [100, 300]]], "kpis": ["frequency.y", "city.load.p[2]"]}, "lhc_size": 10, "number_of_runs": 10}

formatted_json_str = pprint.pformat(json_dict)
print(formatted_json_str)
pprint.pprint(json_dict)

Result of pprint.pformat(...) or pprint.pprint(...):

{'hyperspace': {'constraints': [],
                'design': [['windFarm.windparkSize.k', 'continuous', [0, 0, 5]],
                           ['hydroPlant.primaryControlMax',
                            'continuous',
                            [100, 300]]],
                'kpis': ['frequency.y', 'city.load.p[2]']},
 'lhc_size': 10,
 'number_of_runs': 10}

Solution 6 - Python

The cli command I've used with python for this is:

cat myfile.json | python -mjson.tool

You should be able to find more info here:

http://docs.python.org/library/json.html

Solution 7 - Python

It looks like jsbeautifier open sourced their tools and packaged them as Python and JS libs, and as CLI tools. It doesn't look like they call out to a web service, but I didn't check too closely. See the github repo with install instructions.


From their docs for Python CLI and library usage:

To beautify using python:

$ pip install jsbeautifier
$ js-beautify file.js

Beautified output goes to stdout.

To use jsbeautifier as a library is simple:

import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')

...or, to specify some options:

opts = jsbeautifier.default_options()
opts.indent_size = 2
res = jsbeautifier.beautify('some javascript', opts)


If you want to pass a string instead of a filename, and you are using bash, then you can use process substitution like so:

$ js-beautify <(echo '{"some": "json"}')

Solution 8 - Python

alias jsonp='pbpaste | python -m json.tool'

This will pretty print JSON that's on the clipboard in OSX. Just Copy it then call the alias from a Bash prompt.

Solution 9 - Python

You could pipe the output to jq. If you python script contains something like

print json.dumps(data)

then you can fire:

python foo.py | jq '.'

Solution 10 - Python

Use the python tool library

Command line: python -mjson.tool

In code: http://docs.python.org/library/json.html

Solution 11 - Python

First install pygments

then

echo '<some json>' | python -m json.tool | pygmentize -l json

Solution 12 - Python

Your data is poorly formed. The value fields in particular have numerous spaces and new lines. Automated formatters won't work on this, as they will not modify the actual data. As you generate the data for output, filter it as needed to avoid the spaces.

Solution 13 - Python

With jsonlint (like xmllint):

aptitude install python-demjson
jsonlint -f foo.json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKhwaishienView Question on Stackoverflow
Solution 1 - PythonSamat JainView Answer on Stackoverflow
Solution 2 - PythonEtienne PerotView Answer on Stackoverflow
Solution 3 - PythonarnuschkyView Answer on Stackoverflow
Solution 4 - PythonDave DopsonView Answer on Stackoverflow
Solution 5 - PythonXiOView Answer on Stackoverflow
Solution 6 - PythonHermsView Answer on Stackoverflow
Solution 7 - PythonDan RossView Answer on Stackoverflow
Solution 8 - PythondavidbittonView Answer on Stackoverflow
Solution 9 - PythonDrorView Answer on Stackoverflow
Solution 10 - PythonbitbucketView Answer on Stackoverflow
Solution 11 - PythonMeng ZhaoView Answer on Stackoverflow
Solution 12 - Pythonzim2411View Answer on Stackoverflow
Solution 13 - PythonJimView Answer on Stackoverflow