iT邦幫忙

DAY 11
8

現代環境下的 Linux 裡的新工具系列 第 11

觀看 JSON 的新工具:jq

  • 分享至 

  • xImage
  •  

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/


上一篇
更進化的命令模式多工器:tmux
下一篇
簡易的網路速度監測工具:nload
系列文
現代環境下的 Linux 裡的新工具27
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
SunAllen
iT邦研究生 1 級 ‧ 2013-09-26 13:10:53

感覺好像很神,不過目前還看不太懂...

十月底之後,要用時間,好好了解這些新東西了沙發

0

延伸閱讀
七種 JSON 閱讀器

性格妞 iT邦新手 2 級 ‧ 2013-09-26 14:33:08 檢舉

不錯的GUI界面工具搜集!

我要留言

立即登入留言