簡體   English   中英

如何在 Python 中美化 JSON?

[英]How to beautify JSON in Python?

有人可以建議我如何在 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


                    "}
        }
    ] } }

從命令行:

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

輸出:

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

以編程方式,Python 手冊描述了漂亮的打印 JSON

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

使用indent了的說法dumps中發揮作用JSON模塊

從文檔:

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

對通過命令行提供的 json 數據進行着色的最小 Python 內解決方案:

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

輸出示例:

在此處輸入圖片說明

試試underscore-cli

cat myfile.json | underscore print --color

這是一個非常漂亮的工具,可以優雅地對結構化數據進行大量操作、執行 js 代碼片段、填充模板等。它的文檔非常好,經過潤色,可以認真使用。 我寫了它。 :)

我不喜歡 json.dumps(...) 的輸出 -> 根據我的口味,換行符太多了。 我不想使用命令行工具或安裝某些東西。 我終於找到了 Pythons pprint (=漂亮的打印)。 不幸的是,它沒有生成正確的 JSON,但我認為在存儲的數據上有一個用戶友好的 glympse 很有用。

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
}

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)

pprint.pformat(...)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}

我在 python 中使用的 cli 命令是:

cat myfile.json | python -mjson.tool

您應該可以在這里找到更多信息:

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

看起來jsbeautifier開源了他們的工具,並將它們打包為 Python 和 JS 庫以及 CLI 工具。 看起來他們並沒有調用網絡服務,但我沒有仔細檢查。 請參閱帶有安裝說明的github 存儲庫


從他們的 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"}')

您可以將輸出通過管道傳輸到jq 如果你的 python 腳本包含類似的東西

print json.dumps(data)

然后你可以開火:

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

這將漂亮地打印 OSX 中剪貼板上的 JSON。 只需復制它,然后從 Bash 提示符調用別名。

首先安裝pygments

然后

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

使用python工具庫

命令行:python -mjson.tool

在代碼中: http : //docs.python.org/library/json.html

您的數據格式不正確。 特別是值字段有許多空格和換行符。 自動格式化程序對此不起作用,因為它們不會修改實際數據。 在生成用於輸出的數據時,根據需要對其進行過濾以避免出現空格。

使用 jsonlint(如 xmllint):

aptitude install python-demjson
jsonlint -f foo.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM