shell里面的jq

本文介绍了如何使用jq工具处理和验证JSON数据。首先通过brew安装jq,然后通过curl获取GitHub上的JSON数据作为示例。接着,展示了如何将数据保存到文件并验证其合法性,以及如何提取JSON数组中的特定项和键值对。例如,获取最新评论的提交日期。
摘要由CSDN通过智能技术生成

jq能处理的需要是严格的JSON格式数据,JSON对象和JSON字符串是不行的,所以如果要使用jq,需要验证json是否是严格的json,当然,jq提供了相应的命令,下面我们安装并使用吧

安装

brew install jq

1、获取json数据

我们以github上jq项目最新一条评论的JSON数据为例,我们使用curl来获取,代码和结果如下

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=1'

结果:
[
  {
    "sha": "a9f97e9e61a910a374a5d768244e8ad63f407d3e",
    "node_id": "C_kwDOAE3WVdoAKGE5Zjk3ZTllNjFhOTEwYTM3NGE1ZDc2ODI0NGU4YWQ2M2Y0MDdkM2U",
    "commit": {
      "author": {
        "name": "Nicolas Williams",
        "email": "nico@cryptonector.com",
        "date": "2021-10-21T05:17:39Z"
      },
      "committer": {
        "name": "Nicolas Williams",
        "email": "nico@cryptonector.com",
        "date": "2021-10-24T21:23:26Z"
      },
      "message": "Fix accidentally quadratic behavior in _modify\n\nWe need to be careful to not grab an extra reference when mutating data\nstructures because that means we make extra copies.  Doing that every\ntime in `_modify` is really painful, as that function implements `|=`\nand all modify-assign operators.\n\n`jv_setpath()` also grabs additional references, so this is not the only\nfix needed for the modify-assign operators to not be accidentally\nquadratic.\n\nWe have to use `LOADVN` in order to make this fix possible, so we should\nreally byte-code `_modify` rather than jq-code it.  However, as a\nstop-gap to make this fix easier, I'm adding syntax for referring to a\n`$binding` such that you get `LOADVN` instead of `LOADV`.\n\nThis syntax is not meant to be used outside jq's internals!  Therefore\nit is not documented.  I promise to break it later, so don't use it!\n\nTBD, but later:\n\n - Optimize `_modify` for the case where `update` outputs more than one\n   value.\n\n   This is trivial: add a `setpath($p; null)` in the middle of `_modify`\n   before calling `update`.  But unfortunately the VM retains a\n   reference to `value_at_path` for path expression checking, and fixing\n   that will require more work.",
      "tree": {
        "sha": "7883361d86bd97b1bcd2299fa5886182b27cee54",
        "url": "https://api.github.com/repos/stedolan/jq/git/trees/7883361d86bd97b1bcd2299fa5886182b27cee54"
      },
      "url": "https://api.github.com/repos/stedolan/jq/git/commits/a9f97e9e61a910a374a5d768244e8ad63f407d3e",
      "comment_count": 0,
      "verification": {
        "verified": false,
        "reason": "unsigned",
        "signature": null,
        "payload": null
      }
    },
    "url": "https://api.github.com/repos/stedolan/jq/commits/a9f97e9e61a910a374a5d768244e8ad63f407d3e",
    "html_url": "https://github.com/stedolan/jq/commit/a9f97e9e61a910a374a5d768244e8ad63f407d3e",
    "comments_url": "https://api.github.com/repos/stedolan/jq/commits/a9f97e9e61a910a374a5d768244e8ad63f407d3e/comments",
    "author": {
      "login": "nicowilliams",
      "id": 604851,
      "node_id": "MDQ6VXNlcjYwNDg1MQ==",
      "avatar_url": "https://avatars.githubusercontent.com/u/604851?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/nicowilliams",
      "html_url": "https://github.com/nicowilliams",
      "followers_url": "https://api.github.com/users/nicowilliams/followers",
      "following_url": "https://api.github.com/users/nicowilliams/following{/other_user}",
      "gists_url": "https://api.github.com/users/nicowilliams/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/nicowilliams/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/nicowilliams/subscriptions",
      "organizations_url": "https://api.github.com/users/nicowilliams/orgs",
      "repos_url": "https://api.github.com/users/nicowilliams/repos",
      "events_url": "https://api.github.com/users/nicowilliams/events{/privacy}",
      "received_events_url": "https://api.github.com/users/nicowilliams/received_events",
      "type": "User",
      "site_admin": false
    },
    "committer": {
      "login": "nicowilliams",
      "id": 604851,
      "node_id": "MDQ6VXNlcjYwNDg1MQ==",
      "avatar_url": "https://avatars.githubusercontent.com/u/604851?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/nicowilliams",
      "html_url": "https://github.com/nicowilliams",
      "followers_url": "https://api.github.com/users/nicowilliams/followers",
      "following_url": "https://api.github.com/users/nicowilliams/following{/other_user}",
      "gists_url": "https://api.github.com/users/nicowilliams/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/nicowilliams/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/nicowilliams/subscriptions",
      "organizations_url": "https://api.github.com/users/nicowilliams/orgs",
      "repos_url": "https://api.github.com/users/nicowilliams/repos",
      "events_url": "https://api.github.com/users/nicowilliams/events{/privacy}",
      "received_events_url": "https://api.github.com/users/nicowilliams/received_events",
      "type": "User",
      "site_admin": false
    },
    "parents": [
      {
        "sha": "0c3455d3290fa03da8c01c135dd7126a80ed28a8",
        "url": "https://api.github.com/repos/stedolan/jq/commits/0c3455d3290fa03da8c01c135dd7126a80ed28a8",
        "html_url": "https://github.com/stedolan/jq/commit/0c3455d3290fa03da8c01c135dd7126a80ed28a8"
      }
    ]
  }
]

我们把获取的一条数据保存在一个st.txt里面,方面后面jq的操作

2、验证json合法性

使用jq .即可验证合法性,并且高亮显示内容

cat st | jq .

结果:
在这里插入图片描述

3、获取数组中的某一项

cat st | jq '.[0]'

由于st文件里面只有一项,所以就输出一项

4、获取某项key-value的值,比如获取提交的日期

➜ cat st | jq '.[0] | {date: .commit.author.date}'
{
  "date": "2021-10-21T05:17:39Z"
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员的修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值