移动时代走哪都少不了跟 API 打交道,既然要用 API 那其实也就少不了跟 JSON 打交道,因为 JSON 其实已经成为了 API 的默认标准传输格式。说到调试 API 接口,相信不少同学都用过类似 Postman,HTTPie,或者简单粗暴的 Curl 这样的调试工具,不过今天要给大家推荐的这个利器,其实严格意义上不算是 API 调试工具,它只是一个专门用来流式处理 JSON 文件的小工具。
安装
# Mac OS
brew install jq
# Ubuntu
sudo apt install -y jq
使用
# 查看文件的全部数据
$ cat haha.json | jq .
[
{
"name": "library",
"update_time": "2018-12-25T09:53:13Z",
"commits": "201",
"branch": "1",
"release": "0",
"contributors": ""
},
{
"name": "lou_challenge_ele",
"update_time": "2018-08-14T08:22:03Z",
"commits": "5",
"branch": "1",
"release": "0",
"contributors": ""
},
{
"name": "shiyanlou-docs",
"update_time": "2018-11-26T01:47:03Z",
"commits": "12",
"branch": "1",
"release": "0",
"contributors": "1"
}
]
# 查看某个字段的数据
$ cat haha.json | jq '.[].name'
"library"
"lou_challenge_ele"
"shiyanlou-docs"
# 查看某一条数据
$ cat c.json | jq '.[0]'
{
"name": "library",
"update_time": "2018-12-25T09:53:13Z",
"commits": "201",
"branch": "1",
"release": "0",
"contributors": ""
}