如何在 Python 中美化 JSON?
Posted
技术标签:
【中文标题】如何在 Python 中美化 JSON?【英文标题】:How to beautify JSON in Python? 【发布时间】:2012-02-24 16:14:26 【问题描述】:有人可以建议我如何在 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
"
]
【问题讨论】:
我喜欢这个问题的标题(美化):) 【参考方案1】:我不喜欢 json.dumps(...) 的输出 -> 因为我的口味太多换行了。而且我不想使用命令行工具或安装一些东西。 我终于找到了 Pythons pprint(= 漂亮的印刷品)。 不幸的是,它不会生成正确的 JSON,但我认为对存储的数据进行用户友好的浏览很有用。
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
【讨论】:
【参考方案2】:首先安装pygments
然后
echo '<some json>' | python -m json.tool | pygmentize -l json
【讨论】:
【参考方案3】:看起来jsbeautifier 开源了他们的工具并将它们打包为 Python 和 JS 库以及 CLI 工具。他们看起来不像是在调用网络服务,但我没有仔细检查。请参阅带有安装说明的 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"')
【讨论】:
【参考方案4】:一个最小的 in-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
。
输出示例:
【讨论】:
非常有用。正是我想要的——在 ipython 中美化 JSON 数据输出。谢谢。 最佳答案在这里,它还包括一行 using json.dumps() 在 python3.7 上与 aiohttp 一起使用,不需要使用 sys 模块和 unicode(String, encoding)。基本上它变成 == highlight(formatted_json, lexers.JsonLexer(), formatters.TerminalFormatter()) 对于 Python3,我只需要删除 unicode() 调用,因为字符串在 Python3 中默认为 Unicode。 不错且简单的解决方案。【参考方案5】:使用 jsonlint(如 xmllint):
aptitude install python-demjson
jsonlint -f foo.json
【讨论】:
【参考方案6】:您可以将输出通过管道传输到jq
。如果你的 python 脚本包含类似的东西
print json.dumps(data)
然后你就可以开火了:
python foo.py | jq '.'
【讨论】:
jq 是一个很棒的工具。【参考方案7】:从命令行:
echo '"one":1,"two":2' | python -mjson.tool
哪个输出:
"one": 1,
"two": 2
以编程方式,Python 手册describes pretty-printing JSON:
>>> import json
>>> print json.dumps('4': 5, '6': 7, sort_keys=True, indent=4)
"4": 5,
"6": 7
【讨论】:
【参考方案8】:试试underscore-cli:
cat myfile.json | underscore print --color
这是一个非常漂亮的工具,可以优雅地对结构化数据进行大量操作,执行 js sn-ps,填充模板等。它的文档非常好,经过优化,可以认真使用。我写了它。 :)
【讨论】:
【参考方案9】:alias jsonp='pbpaste | python -m json.tool'
这将漂亮地打印出 OSX 剪贴板上的 JSON。只需复制它,然后从 Bash 提示符调用别名。
【讨论】:
【参考方案10】:您的数据格式不正确。特别是值字段有许多空格和新行。自动格式化程序对此不起作用,因为它们不会修改实际数据。在生成输出数据时,根据需要对其进行过滤以避免出现空格。
【讨论】:
【参考方案11】:在json module 中使用dumps
函数的indent
参数。
来自文档:
>>> import json
>>> print json.dumps('4': 5, '6': 7, sort_keys=True, indent=4)
"4": 5,
"6": 7
【讨论】:
【参考方案12】:使用python工具库
命令行:python -mjson.tool
在代码中:http://docs.python.org/library/json.html
【讨论】:
【参考方案13】:我使用 python 的 cli 命令是:
cat myfile.json | python -mjson.tool
您应该可以在此处找到更多信息:
http://docs.python.org/library/json.html
【讨论】:
这是另一个虐待猫的不幸例子。无需创建单独的进程和一堆无偿的内存复制就可以实现相同的结果,只需一个简单的python -mjson.tool < myfile.json
也许我喜欢喂我的猫? :P
在缩进当前行:.!python -mjson.tool
时与vim配合得很好以上是关于如何在 Python 中美化 JSON?的主要内容,如果未能解决你的问题,请参考以下文章