JSON 是近幾年興起常用的格式,檢視JSON格式的工具也當然是新工具。
JSON是API格式的潮流
什麼是JSON?可參考本站的 白話說JSON。過去交換資料時,常以XML格式為主要考慮,但繁冗的成對標籤不易讀寫,而JSON的格式幾乎可以勝任XML交換資料的功能,且更易讀寫,而成為現在API常見的輸出格式。
網站JSON實例
以 http://www.reddit.com/r/json/ 這著名的推文網站為例,相對於網頁也提供了 JSON 的格式,網址為 http://www.reddit.com/r/json.json,看到的內容像是這樣:
如果需要了解其結構,來做第三方的App開發,資料一多就很難去認key,value的關係,這時這兩個工具是同樣的功能,讓JSON的格式排成較易閱讀的方式來觀看:
jq
執行結果
$ curl http://www.reddit.com/r/json.json | jq '.'
{
"data": {
"before": null,
"after": null,
"children": [
{
"data": {
"distinguished": null,
"num_reports": null,
"num_comments": 0,
"ups": 1,
"link_flair_text": null,
"created_utc": 1378360322,
"score": 1,
"media": null,
"author": "based2",
"stickied": false,
"clicked": false,
"secure_media_embed": {},
"id": "1lrode",
"saved": false,
"domain": "bestiejs.github.io",
"banned_by": null,
"media_embed": {},
"subreddit": "json",
"selftext_html": null,
"selftext": "",
"likes": null,
"secure_media": null,
"approved_by": null,
"over_18": false,
"hidden": false,
"thumbnail": "",
"subreddit_id": "t5_2sh4w",
"edited": false,
"link_flair_css_class": null,
"author_flair_css_class": null,
"downs": 0,
"is_self": false,
"permalink": "/r/json/comments/1lrode/json3_interop_security_and_performance_benefits/",
"name": "t3_1lrode",
"created": 1378363922,
"url": "http://bestiejs.github.io/json3/",
"author_flair_text": null,
"title": "Json3 interop: security and performance benefits in obsolete and mobile environment"
},
"kind": "t3"
},
...
{
"data": {
"distinguished": null,
"num_reports": null,
"num_comments": 0,
"ups": 2,
"link_flair_text": null,
"created_utc": 1304663230,
"score": 2,
"media": null,
"author": "[deleted]",
"stickied": false,
"clicked": false,
"secure_media_embed": {},
"id": "h5flz",
"saved": false,
"domain": "github.com",
"banned_by": null,
"media_embed": {},
"subreddit": "json",
"selftext_html": null,
"selftext": "",
"likes": null,
"secure_media": null,
"approved_by": null,
"over_18": false,
"hidden": false,
"thumbnail": "",
"subreddit_id": "t5_2sh4w",
"edited": false,
"link_flair_css_class": null,
"author_flair_css_class": null,
"downs": 0,
"is_self": false,
"permalink": "/r/json/comments/h5flz/the_latest_version_of_the_original_json_library/",
"name": "t3_h5flz",
"created": 1304666830,
"url": "https://github.com/douglascrockford/JSON-js",
"author_flair_text": null,
"title": "The latest version of the original JSON library by Douglas Crockford, on github"
},
"kind": "t3"
}
],
"modhash": ""
},
"kind": "Listing"
}
依據key value做縮格階層方式顯示而更易讀。
jq的過濾語法
如果想找出reddit 裡 JSON的各title的值怎麼去跑?
該JSON內容太多,先從簡單的例子測試:
這網址可看自己IP的相關資訊成JSON格式http://ifconfig.me/all.json
$ curl -s http://ifconfig.me/all.json | jq '.'
{
"encoding": "",
"keep_alive": "",
"mime": "*/*",
"forwarded": "",
"connection": "Keep-Alive",
"ip_addr": "1.2.3.4",
"lang": "",
"remote_host": "a.b.c.d",
"user_agent": "curl/7.32.0",
"charset": "",
"port": "52968",
"via": ""
}
若只要取 user_agent 的值,用這樣的指令獲取:
curl -s http://ifconfig.me/all.json | jq '.user_agent'
其他的語法參考 jq的手冊 很有趣,試試例子,可以了解怎麼去存取 JSON 裡的某些值或運算出所要的結果。
所以要獲取各title可以用這樣的語法:
$ curl -s http://www.reddit.com/r/json.json |jq '.data.children[].data.title'
"Json3 interop: security and performance benefits in obsolete and mobile environment"
"A new MMORPG (currently on Kickstarter) plans to allow client/server access to data via JSON."
"Json to HTML table "
"Why JSON Won ... And Is Good As It Is"
"JSON editor jQuery plugin"
"HN: JSON Patch, a proposed standard for synchronizing JSON structures (json xslt)"
"The JSON Date Gotcha - [codephined.com]"
"The latest version of the original JSON library by Douglas Crockford, on github"
也許不見得一定要把jq的語法弄熟,重要的是自己會用到的開發程式,熟悉怎麼從JSON抓資料就好,jq只是做為觀察的用途。
參考教學:
http://stedolan.github.io/jq/tutorial/