JSONとCSVを行き来する
- 整える
- 変換する
JSONは、プログラムには便利でも、人が集計するには、表のほうが向いています。逆に、表のデータを、プログラムに渡すにはJSONが必要です。JSONを整える・チェックする で整えたJSONを、CSVとの変換ツールと組み合わせる方法です。
流れ(JSON → 表)
- このツールで、JSONを整えて、正しいか確認する。
- 整ったJSONを、CSV・JSONの変換ツールの「JSON → CSV」に貼り付ける。
- CSVに変換して保存し、Excelで開く。
流れ(表 → JSON)
- Excelの表をコピーして、CSV・JSONの変換ツールで「CSV → JSON」に変換する。
- できたJSONを、このツールで整えて、正しいか確認する。
- 必要なら「1行にまとめる」で、詰める。
変換できるJSONの形
同じ項目を持つデータが、並んでいる形が、表に変換しやすいです。入れ子の部分は、表に変換すると、1つのセルに文字として入ります。
組み合わせるメリット
- 変換のエラーを、事前に防げる
- 整形で、データの構造を理解してから、変換できる
- 変換後のJSONも、すぐに整形できる
向いている場面
- APIの結果を、Excelで集計する
- Excelのデータを、Webアプリのデータにする
- 設定データの、表での管理
注意すること
- 表で扱えるのは、平らな構造です。深い入れ子は、先に平らにするか、必要な部分を抜き出します。
- 数値と文字列の区別が、変換で変わる場合があります。
例
APIの結果のJSONを、このツールで整えて構造を確認した。必要な項目が入れ子の外にあると分かったので、そのままCSVに変換して、Excelで集計できた。
つまずきやすい点
- 入れ子の項目が、1つのセルに入る:先に整形する。
- 日付や数値の書式が変わる:Excel側で書式を設定する。
- 文字化け:Excelで開くときの、文字コードの指定。
よくある質問
2つのツールの順番は、どちらが先ですか?
JSONを受け取ったら、まず整形・チェック。それから変換です。
まとめ
JSONの整形・チェックと、CSVへの変換を、組み合わせて使いましょう。
JSONを整える・チェックするを使う(無料)あわせて使えるツール
- CSVとJSONを相互変換する:CSVとJSONを、すぐに相互変換します。
- ファイルとBase64を相互変換する:ファイルとBase64の文字を相互に変換します。
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
同じツールの、ほかの解説
Going Between JSON and CSV
- Format
- Convert
JSON is convenient for programs but a table suits people totalling things. Conversely, table data needs to be JSON to be handed to a program. Here is how to combine JSON formatted with Format and Validate JSON and the CSV conversion tool.
Flow (JSON → table)
- Format the JSON with this tool and check it's correct.
- Paste the tidy JSON into "JSON → CSV" of the CSV/JSON conversion tool.
- Convert to CSV, save and open in Excel.
Flow (table → JSON)
- Copy the Excel table and convert it with "CSV → JSON" in the CSV/JSON conversion tool.
- Format the resulting JSON with this tool and check it's correct.
- If needed, compress with "Make one line".
JSON shapes that can be converted
A form where data with the same items is lined up is easy to convert to a table. Nested parts, when converted to a table, go into one cell as text.
Benefits of combining
- Conversion errors can be prevented in advance
- You can understand the data's structure through formatting before converting
- The converted JSON can be formatted at once
Good for
- Totalling API results in Excel
- Turning Excel data into web app data
- Managing configuration data as a table
Cautions
- What a table can handle is flat structures. Flatten deep nesting first, or extract the needed parts.
- The distinction between numbers and strings can change in conversion.
Example
Formatted JSON from an API with this tool to see its structure. Finding that the needed items were outside the nesting, it converted straight to CSV and could be totalled in Excel.
Common stumbling blocks
- Nested items go into one cell: reshape first.
- Date and number formats change: set the format on the Excel side.
- Garbled text: specify the encoding when opening in Excel.
Frequently asked questions
Which tool comes first?
On receiving JSON, first format and check. Then convert.
Summary
Use JSON formatting and checking in combination with conversion to CSV.
Use "Format and Validate JSON" (free)Tools that go well with this
- Convert CSV and JSON: Convert between CSV and JSON instantly.
- Convert Files and Base64: Convert between files and Base64 text.
- Remove Duplicate Lines and Sort a List: Remove duplicate lines and sort them.