12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jqコマンドまとめメモ

Last updated at Posted at 2016-05-23

##jqコマンドめも
・コマンドで指定するときの.(ドット)は項目名を表している
・コマンドで指定するときは項目を並べることで階層をたどっている(ドキュメントルートでいう/みたいな)
例えば $ jq -r '.foo.bar[]' でbar以下の情報までたどる
参考:http://qiita.com/takeshinoda@github/items/2dec7a72930ec1f658af
元データはこれ(ネットでひろったやつ)

{
  "foo": {
    "bar": [
      {
        "key": "1-key",
        "value": "1-value"
      },
      {
        "key": "2-key",
        "value": "2-value",
        "option": "2-opt"
      }
    ],
    "baz": [
      {
        "key": "3-key",
        "value": "3-value",
        "option": "3-opt"
      },
      {
        "key": "4-key",
        "value": "4-value"
      }
    ]
  }
}

スクリプト用データとしてCSV出力がしたい場合

$ cat sample.json | jq -r '.foo.bar[]'
{
  "key": "1-key",
  "value": "1-value"
}
{
  "key": "2-key",
  "value": "2-value",
  "option": "2-opt"
}

要素を指定して表示させる

$ cat sample.json | jq -r '.foo.bar[]|[.key, .value]'
[
  "1-key",
  "1-value"
]
[
  "2-key",
  "2-value"
]

###[]番目の配列として表示(配列に入れる)

$ cat sample.json | jq -r '.foo.bar[0]|[.key, .value]'
[
  "1-key",
  "1-value"
]

###配列に入れてcsvで出力

$ cat sample.json | jq -r '.foo.bar[0]|[.key, .value]|@csv'
"1-key","1-value"
 
$ cat sample.json | jq -r '.foo.bar[]|[.key, .value]|@csv'
"1-key","1-value"
"2-key","2-value"

###csvで出力したものをawkで成形
$ awk -F, '{print ($1)}'
これでcsvの1列目だけを表示する

$ cat sample.json | jq -r '.foo.bar[]|[.key, .value]|@csv' | awk -F, '{print $1}'
"1-key"
"2-key"
 
$ cat sample.json | jq -r '.foo.bar[]|[.key, .value]|@csv' | awk -F, '{print $1,$2}'
"1-key" "1-value"
"2-key" "2-value"

### ""が邪魔なのでsedで置き換える

$ cat sample.json | jq -r '.foo.bar[]|[.key, .value]|@csv' | awk -F, '{print $1,$2}' | sed -e 's/"//g'
1-key 1-value
2-key 2-value

$ cat sample.json | jq -r '.foo.baz[]|[.key, .value]|@csv' | awk -F, '{print $1,$2}' | sed -e 's/"//g'
3-key 3-value
4-key 4-value

.foo.barじゃなくて.foo.bazをとりたい

$ cat sample.json  | jq -r '.foo[]'
[
  {
    "key": "1-key",
    "value": "1-value"
  },
  {
    "key": "2-key",
    "value": "2-value",
    "option": "2-opt"
  }
]
[
  {
    "key": "3-key",
    "value": "3-value",
    "option": "3-opt"
  },
  {
    "key": "4-key",
    "value": "4-value"
  }
]

$ cat sample.json  | jq -r '.foo[][]'
{
  "key": "1-key",
  "value": "1-value"
}
{
  "key": "2-key",
  "value": "2-value",
  "option": "2-opt"
}
{
  "key": "3-key",
  "value": "3-value",
  "option": "3-opt"
}
{
  "key": "4-key",
  "value": "4-value"
}

$ cat sample.json  | jq -r '.foo[][] | [.key , .value , .option] | @csv'
"1-key","1-value",
"2-key","2-value","2-opt"
"3-key","3-value","3-opt"
"4-key","4-value",
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?