是否有(Unix)shell脚本将JSON格式化为可读格式?
基本上,我希望它转换以下内容:
{ "foo": "lorem", "bar": "ipsum" }
…变成这样:
{
"foo": "lorem",
"bar": "ipsum"
}
是否有(Unix)shell脚本将JSON格式化为可读格式?
基本上,我希望它转换以下内容:
{ "foo": "lorem", "bar": "ipsum" }
…变成这样:
{
"foo": "lorem",
"bar": "ipsum"
}
当前回答
我通常只做:
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
其他回答
你只需要使用jq如果未安装jq,则需要先安装jq。
sudo apt-get update
sudo apt-get install jq
安装jq后,只需使用jq
echo '{ "foo": "lorem", "bar": "ipsum" }' | jq
输出看起来像
{
"foo": "lorem",
"bar": "ipsum"
}
此外,请务必查看JSONFUI:一个支持折叠的命令行JSON查看器
我正在使用httpie
$ pip install httpie
你可以这样使用
$ http PUT localhost:8001/api/v1/ports/my
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 93
Content-Type: application/json
Date: Fri, 06 Mar 2015 02:46:41 GMT
Server: nginx/1.4.6 (Ubuntu)
X-Powered-By: HHVM/3.5.1
{
"data": [],
"message": "Failed to manage ports in 'my'. Request body is empty",
"success": false
}
或者,使用Ruby:
echo '{ "foo": "lorem", "bar": "ipsum" }' | ruby -r json -e 'jj JSON.parse gets'
你也可以选择使用在线工具。
我发现http://jsonprettyprint.net是最简单和最简单的。