如何漂亮打印JSON? [英] How can I pretty-print JSON?

查看:363
本文介绍了如何漂亮打印JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个(unix)shell脚本来以人类可读的形式来格式化JSON?



基本上,我想让它转换下面的内容:

  {foo:lorem,bar:ipsum} 



...如下:

  {
foo:lorem,
bar:ipsum
}


  echo' {foo:lorem,bar:ipsum}'| python -m json.tool 

或者,如果JSON在文件中,您可以: / p>

  python -m json.tool my_json.json 

如果JSON来自互联网源(如API),您可以使用

  curl http:// my_url / | python -m json.tool 

为了方便起见,可以使用别名: p>

 别名prettyjson ='python -m json.tool'


Is there a (unix) shell script to format JSON in human-readable form?

Basically, I want it to transform the following:

{ "foo": "lorem", "bar": "ipsum" }

... into something like this:

{
    "foo": "lorem",
    "bar": "ipsum"
}

解决方案

With Python 2.6+ you can just do:

echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool

or, if the JSON is in a file, you can do:

python -m json.tool my_json.json

if the JSON is from an internet source such as an API, you can use

curl http://my_url/ | python -m json.tool

for convenience in all of these cases you can make an alias:

alias prettyjson='python -m json.tool'

这篇关于如何漂亮打印JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆