shell里面的jq
生活随笔
收集整理的這篇文章主要介紹了
shell里面的jq
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
jq能處理的需要是嚴格的JSON格式數(shù)據(jù),JSON對象和JSON字符串是不行的,所以如果要使用jq,需要驗證json是否是嚴格的json,當然,jq提供了相應的命令,下面我們安裝并使用吧
安裝
brew install jq1、獲取json數(shù)據(jù)
我們以github上jq項目最新一條評論的JSON數(shù)據(jù)為例,我們使用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"}]} ]我們把獲取的一條數(shù)據(jù)保存在一個st.txt里面,方面后面jq的操作
2、驗證json合法性
使用jq .即可驗證合法性,并且高亮顯示內容
cat st | jq .結果:
3、獲取數(shù)組中的某一項
cat st | jq '.[0]'由于st文件里面只有一項,所以就輸出一項
4、獲取某項key-value的值,比如獲取提交的日期
? cat st | jq '.[0] | {date: .commit.author.date}' {"date": "2021-10-21T05:17:39Z" }總結
以上是生活随笔為你收集整理的shell里面的jq的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 人生必看的一百部电影
- 下一篇: fama matlab源码_Matlab