命令行json - 如何在shell脚本中打印JSON?
shell jq json (20)
是否有(Unix)shell脚本以人类可读的形式格式化JSON?
基本上,我希望它改变以下内容:
{ "foo": "lorem", "bar": "ipsum" }
...进入这样的事情:
{
"foo": "lorem",
"bar": "ipsum"
}
香草巴什
一个简单的Bash脚本( grep
/ awk
),用于漂亮的JSON打印,没有第三方安装:
json_pretty.sh
#/bin/bash
grep -Eo '"[^"]*" *(: *([0-9]*|"[^"]*")[^{}\["]*|,)?|[^"\]\[\}\{]*|\{|\},?|\[|\],?|[0-9 ]*,?' | awk '{if ($0 ~ /^[}\]]/ ) offset-=4; printf "%*c%s\n", offset, " ", $0; if ($0 ~ /^[{\[]/) offset+=4}'
例子:
1)在控制台中读取文件和漂亮的打印
cat file.json | json_pretty.sh
2)使用Windows GIT Bash从文件到文件(基于UTF8):
cat fileIn.json |sh.exe json_pretty.sh > fileOut.json
JSON Ruby Gem与shell脚本捆绑在一起来美化JSON:
sudo gem install json
echo '{ "foo": "bar" }' | prettify_json.rb
脚本下载: gist.github.com/3738968
PHP版本,如果你有PHP> = 5.4。
alias prettify_json=php -E '$o = json_decode($argn); print json_encode($o, JSON_PRETTY_PRINT);'
echo '{"a":1,"b":2}' | prettify_json
jj是超快的,可以经济地处理巨大的JSON文档,不会弄乱有效的JSON数字,并且易于使用,例如
jj -p # for reading from STDIN
要么
jj -p -i input.json
它(2018)仍然很新,所以也许它不会按照你期望的方式处理无效的JSON,但它很容易在主要平台上安装。
使用Perl,如果从CPAN安装JSON::PP ,您将获得json_pp命令。 窃取B Bycroft的example你会得到:
[[email protected] ~]$ echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp
{
"bar" : "ipsum",
"foo" : "lorem"
}
值得一提的是, json_pp
预先安装了Ubuntu 12.04(至少)和Debian /usr/bin/json_pp
在/usr/bin/json_pp
使用Python 2.6+,你可以做到:
echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool
或者,如果JSON在文件中,您可以:
python -m json.tool my_json.json
如果JSON来自互联网源,例如API,您可以使用
curl http://my_url/ | python -m json.tool
为了方便所有这些情况,您可以创建一个别名:
alias prettyjson='python -m json.tool'
为了更方便,更多的打字准备就绪:
prettyjson_s() {
echo "$1" | python -m json.tool
}
prettyjson_f() {
python -m json.tool "$1"
}
prettyjson_w() {
curl "$1" | python -m json.tool
}
对于所有上述情况。 你可以将它放在.bashrc
,它每次都可以在shell中使用。 像prettyjson_s '{"foo": "lorem", "bar": "ipsum"}'
一样调用它。
只需将输出传输到jq .
。
例:
twurl -H ads-api.twitter.com '.......' | jq .
在* nix上,从stdin读取并写入stdout更好:
#!/usr/bin/env python
"""
Convert JSON data to human-readable form.
(Reads from stdin and writes to stdout)
"""
import sys
try:
import simplejson as json
except:
import json
print json.dumps(json.loads(sys.stdin.read()), indent=4)
sys.exit(0)
把它放在一个文件中(我在AnC的回答中将其命名为“prettyJSON”)在你的PATH和chmod +x
,然后你就可以了。
感谢JF Sebastian的非常有用的指示,这里有一个稍微增强的脚本我想出了:
#!/usr/bin/python
"""
Convert JSON data to human-readable form.
Usage:
prettyJSON.py inputFile [outputFile]
"""
import sys
import simplejson as json
def main(args):
try:
if args[1] == '-':
inputFile = sys.stdin
else:
inputFile = open(args[1])
input = json.load(inputFile)
inputFile.close()
except IndexError:
usage()
return False
if len(args) < 3:
print json.dumps(input, sort_keys = False, indent = 4)
else:
outputFile = open(args[2], "w")
json.dump(input, outputFile, sort_keys = False, indent = 4)
outputFile.close()
return True
def usage():
print __doc__
if __name__ == "__main__":
sys.exit(not main(sys.argv))
我使用JSON.stringify
的“space”参数在JavaScript中漂亮地打印JSON。
例子:
// Indent with 4 spaces
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, 4);
// Indent with tabs
JSON.stringify({"foo":"lorem","bar":"ipsum"}, null, '\t');
从带有nodejs的Unix命令行,在命令行上指定json:
$ node -e "console.log(JSON.stringify(JSON.parse(process.argv[1]), null, '\t'));" \
'{"foo":"lorem","bar":"ipsum"}'
返回:
{
"foo": "lorem",
"bar": "ipsum"
}
从带有Node.js的Unix命令行,指定包含JSON的文件名,并使用四个空格的缩进:
$ node -e "console.log(JSON.stringify(JSON.parse(require('fs') \
.readFileSync(process.argv[1])), null, 4));" filename.json
使用管道:
echo '{"foo": "lorem", "bar": "ipsum"}' | node -e \
"\
s=process.openStdin();\
d=[];\
s.on('data',function(c){\
d.push(c);\
});\
s.on('end',function(){\
console.log(JSON.stringify(JSON.parse(d.join('')),null,2));\
});\
"
我写了一个工具,它有一个最好的“智能空白”格式化器。 它比这里的大多数其他选项产生更可读,更简洁的输出。
这就是“智能空白”的样子:
我可能有点偏颇,但它是一个很棒的工具,用于从命令行打印和操作JSON数据。 它使用起来非常友好,并提供广泛的命令行帮助/文档。 这是一把瑞士军刀,我用它来完成1001个不同的小任务,任何其他方式都令人惊讶地烦人。
最新用例:Chrome,开发控制台,网络选项卡,全部导出为HAR文件,“cat site.har |下划线选择'.url' - outfmt text | grep mydomain”; 现在我有一个按时间顺序列出的所有URL提取列表,在加载我公司的网站时。
漂亮的打印很简单:
underscore -i data.json print
一样:
cat data.json | underscore print
同样的事情,更明确:
cat data.json | underscore print --outfmt pretty
这个工具是我目前的激情项目,所以如果您有任何功能请求,我很有可能会解决它们。
我建议使用JSON :: XS perl模块中包含的json_xs命令行实用程序。 JSON :: XS是一个用于序列化/反序列化JSON的Perl模块,在Debian或Ubuntu机器上你可以像这样安装它:
sudo apt-get install libjson-xs-perl
它显然也可以在CPAN 。
要使用它来格式化从URL获取的JSON,您可以使用curl或wget,如下所示:
$ curl -s http://page.that.serves.json.com/json/ | json_xs
或这个:
$ wget -q -O - http://page.that.serves.json.com/json/ | json_xs
并格式化文件中包含的JSON,您可以这样做:
$ json_xs < file-full-of.json
要重新格式化为YAML ,有些人认为它比JSON更具人性化可读性:
$ json_xs -t yaml < file-full-of.json
我通常只做:
echo '{"test":1,"test2":2}' | python -mjson.tool
并检索选择数据(在这种情况下,“测试”的值):
echo '{"test":1,"test2":2}' | python -c 'import sys,json;data=json.loads(sys.stdin.read()); print data["test"]'
如果JSON数据在文件中:
python -mjson.tool filename.json
如果您想使用身份验证令牌在命令行中使用curl
一次性完成所有操作:
curl -X GET -H "Authorization: Token wef4fwef54te4t5teerdfgghrtgdg53" http://testsite/api/ | python -mjson.tool
或者,使用Ruby:
echo '{ "foo": "lorem", "bar": "ipsum" }' | ruby -r json -e 'jj JSON.parse gets'
这就是我这样做的方式:
gem install jazor
jazor --help
它缩短了代码并完成了工作。