在我的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。

其他回答

查看curljson

$ pip install curljson
$ curljson -i <the-json-api-url>

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

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

就是这样!

python -m json.tool
Curl http://127.0.0.1:5000/people/api.json | python -m json.tool

也能有所帮助。

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

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

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

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