[svc]jq神器使用
jq神器
处理json数据
支持过滤某字段
支持数学运算(对字段处理)
- 安装
yum install -y jq
- 使用
参考:
http://blog.just4fun.site/command-tool-jq.html
https://www.ibm.com/developerworks/cn/linux/1612_chengg_jq/index.html
在线jq表达式匹配:
https://jqplay.org/
- 教程
https://stedolan.github.io/jq/tutorial/
过滤超过18岁的
[{
"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))'
json数据的第一项
cat t.tt | jq '.[0]'
- 示例json数据
{
"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"
}
]
}
- 格式化json
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]'
- 按照某些字段重组json
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
- 按照某些字段重组json(抽出所有项)
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
- 按照某些字段重组json(抽出所有项)(结果弄成一个[])
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"
]
}
]