LoginSignup
4
5

More than 5 years have passed since last update.

json to csv by jq command

Posted at

json to csv by jq command

目的

json形式のデータから、目的のデータをcsv形式で出力する。

テストデータと期待する結果

テストデータ

test.json
[
  {
    "actions": [],
    "name": "ペドロ",
    "status": {
      "HP": 100,
      "speed": 50
    }
  },
  {
    "actions": [],
    "name": "ピート",
    "status": {
      "HP": 300,
      "speed": 20
    }
  }
]

上記データからには2人分のデータが格納されているが
それぞれのHPとspeedを抜き出し下記のような出力としたい。

期待する結果

result.csv
100, 50
300, 20

下記コマンドで期待する結果を出力できる

 cat test.json | jq  ". [].status | [.HP,.speed] | @csv" | sed 's/"//g' > result.csv
4
5
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
4
5