9
4

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 3 years have passed since last update.

jqコマンドで、JSON、JSONL形式をCSV形式に変換する

Posted at

はじめに

JSON形式(JSON,JSONL)からCSV形式への変換をたまに必要としますが、毎度忘れるのでまとめておこうと思いました。

JSON形式からJSONL形式に変換

jq -c '.[]' input.json > output.jsonl

JSONL形式からJSON形式に変換

jq -s '.' input.jsonl > output.json

JSON形式からCSV形式に変換

cat output.json | jq -r '.[] | [.col1, .col2, .col3, .col4, .col5, .col6, .col7]|@csv' > json_to_csv.csv

JSON形式からCSV(ヘッダー付き)形式に変換

cat output.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'  > json_to_csv_header.csv

参考

以下のサイトを参考にさせていただきました。問題ありましたらご連絡ください。

9
4
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
9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?