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"
}