在我的Unix shell脚本中,当我执行curl命令时,结果将显示如下,我将重定向到文件:

{"type":"Show","id":"123","title":"name","description":"Funny","channelTitle":"ifood.tv","lastUpdateTimestamp":"2014-04-20T20:34:59","numOfVideos":"15"}

但是,我想把这个输出放在可读的JSON格式如下文件:

{"type":"Show",
"id":"123",
"title":"name",
"description":"Funny",
"channelTitle":"ifood.tv",
"lastUpdateTimestamp":"2014-04-20T20:34:59",
"numOfVideos":"15"}

如何以这种方式格式化输出?


当前回答

xidel:

curl <...> | xidel - -se '$json'

xidel也可以为您检索JSON。

其他回答

你可以安装jq并像下面这样查询:

curl http://localhost:8080/observations/station/221 | jq

我猜您想美化JSON输出。 这可以使用python实现:

curl http://localhost:8880/test.json | python -mjson.tool > out.json

你可以使用json node模块:

NPM I -g json

然后简单地在curl后面追加| json。 Curl http://localhost:8880/test.json | json

除了格式化,还有更多的特性(切片、过滤、映射和结构化转换)。

https://stedolan.github.io/jq/

我发现json_reformat非常方便。所以我做了以下事情:

curl http://127.0.0.1:5000/people/api.json | json_reformat

就是这样!