2012-02-01 63 views
43

有人可以建議我如何在Python中或通過命令行美化JSON嗎?如何在Python中美化JSON?

唯一能夠做到的基於在線的JSON美化器是:http://jsonviewer.stack.hu/

但是,我需要在Python中使用它。

這是我的數據集:

{ "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 


        "} 
     } 
    ] } } 

回答

90

從命令行:

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

,其輸出:

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

Programmtically,Python的手動describes pretty-printing JSON

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

我已經與蟒蛇此使用的CLI命令:

cat myfile.json | python -mjson.tool 

你應該能夠在這裏找到更多的信息:

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

+0

這是貓科動物虐待的另一個不幸的例子。如果不創建一個單獨的進程和一堆無用的內存 - 用一個簡單的'python -mjson.tool 2017-07-13 14:33:02

+0

也許我喜歡餵養我的貓? :P – Herms 2017-07-14 20:09:59

18

json module中使用dumps函數的indent參數。

從文檔:

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

您的數據不佳形成。價值領域特別是有許多空間和新線。自動格式化程序將不會在此工作,因爲它們不會修改實際數據。在生成輸出數據時,根據需要對其進行過濾以避免出現空格。

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

這將很好地打印在OSX剪貼板上的JSON。只需複製它,然後從Bash提示中調用別名。

15

嘗試underscore-cli

cat myfile.json | underscore print --color 

這是一個非常漂亮的工具,可以優雅地做了很多結構化數據的處理的,執行JS代碼片段,填寫模板等,這是可笑的有據可查的,拋光,並準備好認真使用。我寫了它。 :)

0

您可以輸出到jq。如果你的Python腳本包含像

print json.dumps(data) 

那麼你可以火:

python foo.py | jq '.' 
0

隨着jsonlint(如xmllint):

aptitude install python-demjson 
jsonlint -f foo.json 
11

的最小的-python的解決方案,它的顏色JSON數據通過命令行提供:

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) 

受上述pjson的啓發。此代碼需要安裝pygments

輸出例如:

enter image description here

+0

非常有用。正是我在尋找 - 在ipython中美化JSON數據輸出。謝謝。 – noumenon 2015-11-26 14:00:47

4

它看起來像jsbeautifier開源了他們的工具,並把它們打包Python和JS庫,並作爲CLI工具。它看起來並不像他們呼喚Web服務,但我沒有仔細檢查。請參閱github repo並提供安裝說明。


從自己的文件爲Python CLI和庫的使用:

爲了美化使用python:

$ pip install jsbeautifier 
$ js-beautify file.js 

美化輸出爲stdout

要使用jsbeautifier爲庫很簡單:

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

...或者,可以指定一些選項:如果你想傳遞一個字符串,而不是一個

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

文件名,並且您正在使用bash,那麼您可以使用進程替換,如下所示:

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

首先安裝Pygments來做

然後

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