jq | play with JSON - WBowam/wbowam.github.com GitHub Wiki
什么是NetHogs?
开源的命令行工具,类似于Linux的sed, grep 。
用于处理JSON.
安装
$ sudo apt-get install jq
用法
-
不用时,生活如此痛苦
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha ## 结果 {"count":11,"num_pages":6,"results":[{"chinese_name":"哈佛大学","english_name":"Harvard University","usnews_ranking":0,"the_times_ranking":0},{"chinese_name":"北卡大学教堂山分校","english_name":"University of North Carolina-Chapel Hill","usnews_ranking":0,"the_times_ranking":0}],"next":"http://school.shunshunliuxue.com:8000/usa/undergradudates/?page=2&page_size=2&search=ha","page_range":[1,2,3,4,5,6],"previous":null}%
-
基本用法
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .
结果 好看了很多
{ "count": 11, "num_pages": 6, "results": [ { "chinese_name": "哈佛大学", "english_name": "Harvard University", "usnews_ranking": 0, "the_times_ranking": 0 }, { "chinese_name": "北卡大学教堂山分校", "english_name": "University of North Carolina-Chapel Hill", "usnews_ranking": 0, "the_times_ranking": 0 } ], "next": "http://school.shunshunliuxue.com:8000/usa/undergradudates/?page=2&page_size=2&search=ha", "page_range": [ 1, 2, 3, 4, 5, 6 ], "previous": null }
-
.field
的用法-
取出 count 的值
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .count
结果
11
-
取出 num_pages
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .num_pages
结果
6
-
取出 results
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results
结果
[ { "chinese_name": "哈佛大学", "english_name": "Harvard University", "usnews_ranking": 0, "the_times_ranking": 0 }, { "chinese_name": "北卡大学教堂山分校", "english_name": "University of North Carolina-Chapel Hill", "usnews_ranking": 0, "the_times_ranking": 0 } ]
-
-
取出
[]
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq ".[]"
结果
{ "chinese_name": "哈佛大学", "english_name": "Harvard University", "usnews_ranking": 0, "the_times_ranking": 0 } { "chinese_name": "北卡大学教堂山分校", "english_name": "University of North Carolina-Chapel Hill", "usnews_ranking": 0, "the_times_ranking": 0 }
-
遍历 方法一
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq ".[]"|jq .chinese_name
结果
"哈佛大学" "北卡大学教堂山分校"
-
遍历 方法二
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq ".[] | .chinese_name"
结果
"哈佛大学" "北卡大学教堂山分校"
-
与sed一起使用
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq ".[] | .english_name"|sed 's/\ /-/g'
结果
"Harvard-University" "University-of-North-Carolina-Chapel-Hill"
-
输出前使用
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq ".[] | {en_name: .english_name, ch_name: .chinese_name}"
结果
{ "en_name": "Harvard University", "ch_name": "哈佛大学" } { "en_name": "University of North Carolina-Chapel Hill", "ch_name": "北卡大学教堂山分校" }
-
将上述 结果做成list
$ curl http://school.shunshunliuxue.com:8000/usa/undergradudates/\?page_size\=2\&search\=ha |jq .results|jq "[ .[] | {en_name: .english_name, ch_name: .chinese_name}]"
结果
[ { "en_name": "Harvard University", "ch_name": "哈佛大学" }, { "en_name": "University of North Carolina-Chapel Hill", "ch_name": "北卡大学教堂山分校" } ]