在 Unix shell 脚本中以可读的 JSON 格式显示 curl 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27238411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display curl output in readable JSON format in Unix shell script
提问by Jams
In my Unix shell script, when I execute a curl command, the result will be displayed as below which I am redirecting to file:
在我的 Unix shell 脚本中,当我执行 curl 命令时,结果将显示如下,我将重定向到文件:
{"type":"Show","id":"123","title":"name","description":"Funny","channelTitle":"ifood.tv","lastUpdateTimestamp":"2014-04-20T20:34:59","numOfVideos":"15"}
But, I want this output to put in the readable JSON format like below in the file:
但是,我希望这个输出以可读的 JSON 格式放入文件中,如下所示:
{"type":"Show",
"id":"123",
"title":"name",
"description":"Funny",
"channelTitle":"ifood.tv",
"lastUpdateTimestamp":"2014-04-20T20:34:59",
"numOfVideos":"15"}
How do I format the output this way?
如何以这种方式格式化输出?
回答by Gilles Quenot
Try doing this :
尝试这样做:
curl ... | json_pp
or with jqusing the identity filter :
或使用身份过滤器使用jq:
curl ... | jq '.'


curl ... | node <<< "var o = $(cat); console.log(JSON.stringify(o, null, 4));"
回答by 0xbb
I am guessing that you want to prettify the JSON output. That could be achieved using python:
我猜你想美化 JSON 输出。这可以使用python来实现:
curl http://localhost:8880/test.json | python -mjson.tool > out.json
curl http://localhost:8880/test.json | python -mjson.tool > out.json
回答by Vishnu
python -m json.tool
Curl http://127.0.0.1:5000/people/api.json | python -m json.tool
can also help.
也可以帮忙。
回答by NanoNova
You can use this node module
您可以使用此节点模块
[sudo] npm i -g json; // suggest not use root privilege to install node module
[sudo] npm i -g json; // 建议不要使用root权限安装node模块
then simply append |jsonafter curl.
curl http://localhost:8880/test.json |json
然后简单地|json在 curl之后追加。
curl http://localhost:8880/test.json |json
回答by Zhenhua
This is to add to of Gilles' Answer. There are many ways to get this done but personally I prefer something lightweight, easy to remember and universally available (e.g. come with standard LTS installations of your preferred Linux flavor or easy to install) on common *nix systems.
这是对吉尔斯回答的补充。有很多方法可以做到这一点,但我个人更喜欢在常见的 *nix 系统上使用轻量级、易于记忆和普遍可用的东西(例如,带有您喜欢的 Linux 风格的标准 LTS 安装或易于安装)。
Here are the options in their preferred order:
以下是按其首选顺序排列的选项:
Python Json.tool module
Python Json.tool 模块
echo '{"foo": "lorem", "bar": "ipsum"}' | python -mjson.tool
pros: almost available everywhere; cons: no color coding
优点:几乎随处可用;缺点:没有颜色编码
jq (may require one time installation)
jq(可能需要一次安装)
echo '{"foo": "lorem", "bar": "ipsum"}' | jq
cons: needs to install jq; pros: color coding and versatile
缺点:需要安装jq;优点:颜色编码和多功能
json_pp (available in Ubuntu 16.04 LTS)
json_pp(在 Ubuntu 16.04 LTS 中可用)
echo '{"foo": "lorem", "bar": "ipsum"}' | json_pp
For Ruby users
对于 Ruby 用户
gem install jsonpretty
echo '{"foo": "lorem", "bar": "ipsum"}' | jsonpretty
回答by Raptor
I found json_reformat to be very handy. So I just did the following:
我发现 json_reformat 非常方便。所以我只是做了以下事情:
curl http://127.0.0.1:5000/people/api.json | json_reformat
that's it!
就是这样!
回答by avivamg
Motivation:You want to print prettify JSON response after curl command request.
动机:您想在 curl 命令请求后打印美化 JSON 响应。
Solution: json_pp- commandline tool that converts between some input and output formats (one of them is JSON). This program was copied from json_xs and modified. The default input format is json and the default output format is json with pretty option.
解决方案: json_pp- 在某些输入和输出格式(其中之一是 JSON)之间转换的命令行工具。该程序是从 json_xs 复制并修改的。默认输入格式是 json,默认输出格式是带有漂亮选项的 json。
Synposis:
json_pp [-v] [-f from_format] [-t to_format] [-json_opt options_to_json1[,options_to_json2[,...]]]
概要:
json_pp [-v] [-f from_format] [-t to_format] [-json_opt options_to_json1[,options_to_json2[,...]]]
Formula: <someCommand> | json_pp
公式:<someCommand> | json_pp
Example:
示例:
Request
要求
curl -X https://jsonplaceholder.typicode.com/todos/1 | json_pp
Response
回复
{
"completed" : false,
"id" : 1,
"title" : "delectus aut autem",
"userId" : 1
}

