How to decorate JSON in Python?

Can someone tell me how I can decorate JSON in Python or through the command line?

The only JSON online jewelry that could do this 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 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 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 "} } ] } } 
+78
json python command-line
Feb 01 '12 at 23:34
source share
13 answers

From the command line:

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

which outputs:

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

Programmatically, the Python manual describes pretty-printed JSON :

 >>> import json >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) { "4": 5, "6": 7 } 
+151
Feb 01 '12 at 23:45
source share
— -

Use the indent argument of dumps function in json module .

From the docs:

 >>> import json >>> print json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) { "4": 5, "6": 7 } 
+28
Feb 01 2018-12-01T00:
source share

Minimal in-python solution that passes json data via 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 to install pygments .

Output Example:

enter image description here

+25
Aug 19 '15 at 10:54
source share

Try underscore-cli :

 cat myfile.json | underscore print --color 

example.png

This is a pretty elegant tool that can gracefully do a lot of manipulations with structured data, execute js fragments, fill in templates, etc. It is ridiculously well-documented, polished and ready for serious use. And I wrote it. :)

+17
Apr 16 2018-12-12T00:
source share

The cli command I used with python to do this:

 cat myfile.json | python -mjson.tool 

You can find more information here:

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

+10
Feb 01 2018-12-01T00:
source share

It seems that the open source jsbeautifier has installed its tools and packaged them as Python and JS libraries, as well as CLI tools. It doesn't seem like they are accessing the web service, but I did not check too carefully. See the github repository for installation instructions.




From their documentation on the Python CLI and using the library:

To decorate with Python:

 $ pip install jsbeautifier $ js-beautify file.js 

Decorated output goes to standard stdout .

Using jsbeautifier as a library is simple:

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

... or, to indicate some parameters:

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



If you want to pass a string instead of a file name and use bash, you can use process substitution as follows:

 $ js-beautify <(echo '{"some": "json"}') 
+7
May 12 '16 at 9:37 a.m.
source share
 alias jsonp='pbpaste | python -m json.tool' 

This will pretty print JSON, which is on the clipboard in OSX. Just copy it, then call the alias from the Bash prompt.

+4
Feb 14
source share

Install pygments first

then

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

+3
Oct 17 '17 at 3:08 on
source share

You can pass jq output. If the python script contains something like

 print json.dumps(data) 

then you can run:

 python foo.py | jq '.' 
+2
Sep 03 '14 at 7:37
source share

Use python tool library

Command line: python -mjson.tool

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

+1
Feb 01 2018-12-01T00:
source share

Your data is poorly formed. Value fields, in particular, have numerous spaces and newlines. Automatic formats will not work on this, as they will not change the actual data. When you create data for output, filter it as needed to avoid spaces.

+1
01 Feb 2018-12-12T00:
source share

Using jsonlint (e.g. xmllint):

 aptitude install python-demjson jsonlint -f foo.json 
0
Dec 15 '14 at 2:49
source share

I think it is better to parse json earlier to avoid errors:

 def format_response(response): try: parsed = json.loads(response.text) except JSONDecodeError: return response.text return json.dumps(parsed, ensure_ascii=True, indent=4) 
0
Apr 26 '19 at 7:34
source share



All Articles