#author(“2018-07-29T01:48:47+00:00”,“default:kazuma”,“kazuma”) Linuxコマンド

使い方 [#c0827644]

整形 [#jfe85be0]

 $ cat /tmp/test.json | jq
 {
   "_scroll_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
   "took": 1,
   "timed_out": false,
   "_shards": {
     "total": 1,
     "successful": 1,
     "failed": 0
   },
   "hits": {
     "total": 12345,
     "max_score": 1,
     "hits": [
       {
         "_index": "hoge-2018.07.18",
         "_type": "fuga",
         "_id": "nankanoid",
         "_score": 1,
         "_source": {
           "remote": "192.168.1.1",
           "menu_id": 1,
           "order_history_id": 2,
           "ordered_zone_id": 3,
           "zone_id": 4,
           "creative_id": 5,
           "action": "impression",
           "timestamp": "2018-07-18 09",
           "uuid": "XXXXX-XXXX-XXXX-XXXX-XXXXX",
           "created_at": "2018-07-18 09:00:01",
           "lait_uuid": "XXXX-XXXX-XXXX-XXXX-XXXX",
           "device": "pc",
           "browser": "Internet Explorer",
           "os": "Windows"
         }
       }
     ]
   }
 }

整形しない [#o349bfa9]

 $ cat /tmp/test.json | jq -c
 {"_scroll_id":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":12345,"max_score":1,"hits":[{"_index":"hoge-2018.07.18","_type":"fuga","_id":"nankanoid","_score":1,"_source":{"remote":"192.168.1.1","menu_id":1,"order_history_id":2,"ordered_zone_id":3,"zone_id":4,"creative_id":5,"action":"impression","timestamp":"2018-07-18 09","uuid":"XXXXX-XXXX-XXXX-XXXX-XXXXX","created_at":"2018-07-18 09:00:01","lait_uuid":"XXXX-XXXX-XXXX-XXXX-XXXX","device":"pc","browser":"Internet Explorer","os":"Windows"}}]}}

{} の段落ごとに取得する [#c1935707]

例1

 $ cat /tmp/test.json | jq ._shards
 {
  "total": 1,
  "successful": 1,
  "failed": 0
 }

例2

 $ cat /tmp/test.json | jq .hits.hits
 [
  {
    "_index": "hoge-2018.07.18",
    "_type": "fuga",
    "_id": "nankanoid",
    "_score": 1,
    "_source": {
      "remote": "192.168.1.1",
      "menu_id": 1,
      "order_history_id": 2,
      "ordered_zone_id": 3,
      "zone_id": 4,
      "creative_id": 5,
      "action": "impression",
      "timestamp": "2018-07-18 09",
      "uuid": "XXXXX-XXXX-XXXX-XXXX-XXXXX",
      "created_at": "2018-07-18 09:00:01",
      "lait_uuid": "XXXX-XXXX-XXXX-XXXX-XXXX",
      "device": "pc",
      "browser": "Internet Explorer",
      "os": "Windows"
    }
  }
 ]

[] を除外する [#g1cda654]

 $ cat /tmp/test.json | jq .hits.hits[]
 {
  "_index": "hoge-2018.07.18",
  "_type": "fuga",
  "_id": "nankanoid",
  "_score": 1,
  "_source": {
    "remote": "192.168.1.1",
    "menu_id": 1,
    "order_history_id": 2,
    "ordered_zone_id": 3,
    "zone_id": 4,
    "creative_id": 5,
    "action": "impression",
    "timestamp": "2018-07-18 09",
    "uuid": "XXXXX-XXXX-XXXX-XXXX-XXXXX",
    "created_at": "2018-07-18 09:00:01",
    "lait_uuid": "XXXX-XXXX-XXXX-XXXX-XXXX",
    "device": "pc",
    "browser": "Internet Explorer",
    "os": "Windows"
  }
 }

複数の要素を表示する(map) [#u776e93f]

 curl https://XXXXXX/api |  jq '.items[] | { content: .content, date: .date_added }'
 {
  "content": "HOGEHOGE",
  "date": "Mon 23 Jul 2018 01:24:00 +0000"
 }
 {
  "content": "FUGAFUGA",
  "date": "Mon 23 Jul 2018 01:24:08 +0000"
 }

ダブルクォートを除外する [#c1f76316]

 -rオプションでダブルクォートが消える

zshで [] を使うとエラーになる [#yc463665]

 zsh: no matches found: .items[]
 (23) Failed writing body

エスケープしてあげればいける

 jq -r .items\[\]