shell脚本如何方便地处理JSON格式的数据呢,这里介绍一个工具:jq。 html
官方教程简单翻译以下。 git
咱们以github上jq项目最新5条评论的JSON数据为例。获取数据以下: github
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'
结果以下: shell
[ { "sha": "d25341478381063d1c76e81b3a52e0592a7c997f", "commit": { "author": { "name": "Stephen Dolan", "email": "mu@netsoc.tcd.ie", "date": "2013-06-22T16:30:59Z" }, "committer": { "name": "Stephen Dolan", "email": "mu@netsoc.tcd.ie", "date": "2013-06-22T16:30:59Z" }, "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161", "tree": { "sha": "6ab697a8dfb5a96e124666bf6d6213822599fb40", "url": "https://api.github.com/repos/stedolan/jq/git/trees/6ab697a8dfb5a96e124666bf6d6213822599fb40" }, "url": "https://api.github.com/repos/stedolan/jq/git/commits/d25341478381063d1c76e81b3a52e0592a7c997f", "comment_count": 0 }, "url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f", "html_url": "https://github.com/stedolan/jq/commit/d25341478381063d1c76e81b3a52e0592a7c997f", "comments_url": "https://api.github.com/repos/stedolan/jq/commits/d25341478381063d1c76e81b3a52e0592a7c997f/comments", "author": { "login": "stedolan", ...
用 jq '.'便可: json
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
结果: api
[ { "parents": [ { "html_url": "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7", "url": "https://api.github.com/repos/stedolan/jq/commits/54b9c9bdb225af5d886466d72f47eafc51acb4f7", "sha": "54b9c9bdb225af5d886466d72f47eafc51acb4f7" }, { "html_url": "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a", "url": "https://api.github.com/repos/stedolan/jq/commits/8b1b503609c161fea4b003a7179b3fbb2dd4345a", "sha": "8b1b503609c161fea4b003a7179b3fbb2dd4345a" } ], "committer": { "type": "User", "received_events_url": "https://api.github.com/users/stedolan/received_events", "events_url": "https://api.github.com/users/stedolan/events{/privacy}", "repos_url": "https://api.github.com/users/stedolan/repos", "organizations_url": "https://api.github.com/users/stedolan/orgs", ...
这里的评论内容比较多,咱们如今想拿第一个评论。 数组
curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
结果: curl
能够看到,咱们已经拿到了一条完整的评论内容。但咱们真正关心的只是评论内容和用户名,下面来获取这两项内容。 工具
jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
结果: url
{ "name": "Stephen Dolan", "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161" }
咱们能够看到,已经拿到了想要的内容,而且已经按咱们本身定义的格式显示了。
这里 | 后面的内容是之前面的内容为输入的,.commit 中的 . 就是指 .[0] 中的内容。
jq '.[] | {message: .commit.message, name: .commit.committer.name}'
结果:
{ "name": "Stephen Dolan", "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161" } { "name": "Stephen Dolan", "message": "Reject all overlong UTF8 sequences." } { "name": "Stephen Dolan", "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences." } { "name": "Stephen Dolan", "message": "Fix example in manual for `floor`. See #155." } { "name": "Nicolas Williams", "message": "Document floor" }
这里 .[] 获取的是数组中的全部项。
咱们看到,结果是一个个独立的JSON对象,如何把结果组合成一个数组呢?
jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
结果:
[ { "name": "Stephen Dolan", "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161" }, { "name": "Stephen Dolan", "message": "Reject all overlong UTF8 sequences." }, { "name": "Stephen Dolan", "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences." }, { "name": "Stephen Dolan", "message": "Fix example in manual for `floor`. See #155." }, { "name": "Nicolas Williams", "message": "Document floor" } ]
咱们能够看到,只要在上一步的命令中内容的两端加个中括号便可。
最后,咱们若是想获取每一个评论的引用评论的url(在parents节点中,有一个或多个)呢?
jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
结果:
[ { "parents": [ "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7", "https://github.com/stedolan/jq/commit/8b1b503609c161fea4b003a7179b3fbb2dd4345a" ], "name": "Stephen Dolan", "message": "Merge pull request #162 from stedolan/utf8-fixes\n\nUtf8 fixes. Closes #161" }, { "parents": [ "https://github.com/stedolan/jq/commit/ff48bd6ec538b01d1057be8e93b94eef6914e9ef" ], "name": "Stephen Dolan", "message": "Reject all overlong UTF8 sequences." }, { "parents": [ "https://github.com/stedolan/jq/commit/54b9c9bdb225af5d886466d72f47eafc51acb4f7" ], "name": "Stephen Dolan", "message": "Fix various UTF8 parsing bugs.\n\nIn particular, parse bad UTF8 by replacing the broken bits with U+FFFD\nand resychronise correctly after broken sequences." }, { "parents": [ "https://github.com/stedolan/jq/commit/3dcdc582ea993afea3f5503a78a77675967ecdfa" ], "name": "Stephen Dolan", "message": "Fix example in manual for `floor`. See #155." }, { "parents": [ "https://github.com/stedolan/jq/commit/7c4171d414f647ab08bcd20c76a4d8ed68d9c602" ], "name": "Nicolas Williams", "message": "Document floor" } ]
这里用 .parents[].html_url 获取当前项的 parents 节点中的全部项的 html_url 属性的内容,而后两边加个中括号组装成数组输出。
怎么样,通过这个例子能够看出,用jq处理JSON数据仍是很方便强大的吧。
jq能处理的须要是严格的JSON格式数据,JSON对象和JSON字符串是不行的,以下面的两种格式数据jq是不能处理的:
json对象:
{ a: 1, b: { c: "abc" } }
json字符串:
'{"a":1,"b":{"c":"abc"}}'
正确的JSON格式:
{ "a": 1, "b": { "c": "abc" } }
关于什么是JSON格式数据,请参考:http://www.json.org/