處理json數據
支持過濾某字段
支持數學運算(對字段處理)html
yum install -y jq
參考:
http://blog.just4fun.site/command-tool-jq.html
https://www.ibm.com/developerworks/cn/linux/1612_chengg_jq/index.htmllinux
在線jq表達式匹配:
https://jqplay.org/git
https://stedolan.github.io/jq/tutorial/github
[{ "name" : "maotai", "age" : 18, "gender" : "male" }, { "name" : "maotai", "age" : 19, "gender" : "male" }, { "name" : "maotai", "age" : 20, "gender" : "male" }]
cat t.tt | jq -r 'map(select(.age>18))'
cat t.tt | jq '.[0]'
{ "sha": "79ece359819cdd7d033d272af9758ae22204c2ef", "commit": { "author": { "name": "William Langford", "email": "wlangfor@gmail.com", "date": "2017-12-05T01:10:56Z" }, "committer": { "name": "William Langford", "email": "wlangfor@gmail.com", "date": "2017-12-05T01:10:56Z" }, "message": "Fix hang for slurped inputs with trailing newline", "tree": { "sha": "d3b481f3448ecd50bf4aa109fd6564dd923afede", "url": "https://api.github.com/repos/stedolan/jq/git/trees/d3b481f3448ecd50bf4aa109fd6564dd923afede" }, "url": "https://api.github.com/repos/stedolan/jq/git/commits/79ece359819cdd7d033d272af9758ae22204c2ef", "comment_count": 0, "verification": { "verified": false, "reason": "unsigned", "signature": null, "payload": null } }, "url": "https://api.github.com/repos/stedolan/jq/commits/79ece359819cdd7d033d272af9758ae22204c2ef", "html_url": "https://github.com/stedolan/jq/commit/79ece359819cdd7d033d272af9758ae22204c2ef", "comments_url": "https://api.github.com/repos/stedolan/jq/commits/79ece359819cdd7d033d272af9758ae22204c2ef/comments", "author": { "login": "wtlangford", "id": 3422295, "avatar_url": "https://avatars2.githubusercontent.com/u/3422295?v=4", "gravatar_id": "", "url": "https://api.github.com/users/wtlangford", "html_url": "https://github.com/wtlangford", "followers_url": "https://api.github.com/users/wtlangford/followers", "following_url": "https://api.github.com/users/wtlangford/following{/other_user}", "gists_url": "https://api.github.com/users/wtlangford/gists{/gist_id}", "starred_url": "https://api.github.com/users/wtlangford/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/wtlangford/subscriptions", "organizations_url": "https://api.github.com/users/wtlangford/orgs", "repos_url": "https://api.github.com/users/wtlangford/repos", "events_url": "https://api.github.com/users/wtlangford/events{/privacy}", "received_events_url": "https://api.github.com/users/wtlangford/received_events", "type": "User", "site_admin": false }, "committer": { "login": "wtlangford", "id": 3422295, "avatar_url": "https://avatars2.githubusercontent.com/u/3422295?v=4", "gravatar_id": "", "url": "https://api.github.com/users/wtlangford", "html_url": "https://github.com/wtlangford", "followers_url": "https://api.github.com/users/wtlangford/followers", "following_url": "https://api.github.com/users/wtlangford/following{/other_user}", "gists_url": "https://api.github.com/users/wtlangford/gists{/gist_id}", "starred_url": "https://api.github.com/users/wtlangford/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/wtlangford/subscriptions", "organizations_url": "https://api.github.com/users/wtlangford/orgs", "repos_url": "https://api.github.com/users/wtlangford/repos", "events_url": "https://api.github.com/users/wtlangford/events{/privacy}", "received_events_url": "https://api.github.com/users/wtlangford/received_events", "type": "User", "site_admin": false }, "parents": [ { "sha": "f06deb828a318536b85d68280d429c3a70b21259", "url": "https://api.github.com/repos/stedolan/jq/commits/f06deb828a318536b85d68280d429c3a70b21259", "html_url": "https://github.com/stedolan/jq/commit/f06deb828a318536b85d68280d429c3a70b21259" } ] }
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' |jq '[.[0] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]' [ { "message": "Fix hang for slurped inputs with trailing newline", "name": "William Langford", "parents": [ "https://github.com/stedolan/jq/commit/f06deb828a318536b85d68280d429c3a70b21259" ] } ]