在终端命令行下优雅地查看 JSON 文件

移动时代走哪都少不了跟 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": ""
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容