入れ子になったJSONを変換するときの注意
- 貼る
- 確かめる
APIなどのJSONには、データの中に、さらにデータが入った「入れ子」の形が、よくあります。CSVとJSONを相互変換する で、入れ子のJSONをCSVに変換するときの注意をまとめます。
手順
- 「変換の向き」で、「JSON → CSV」を選びます。
- 「変換前」に、JSONを貼り付けます。
- 「変換する」を押します。
- 「変換後」を確認します。
入れ子とは
たとえば、次のようなデータです。
- 名前:山田
- 住所:(都道府県:東京、市区町村:新宿)
「住所」の中に、さらに項目が入っています。これが、入れ子です。
変換したときの結果
CSVは、平らな表です。入れ子の部分は、表の形にできないため、中身が文字としてそのまま、1つのセルに入ります。
平らな形に直す方法
- 項目を展開する:住所の中身を、「都道府県」「市区町村」の別の項目として、外に出す。
- テキストエディタで直す:置換機能などで、構造を変える。
- 必要な部分だけを抜き出す:目的の項目だけの、JSONを作る。
どのくらい直すか
- 入れ子の中の値が必要ない → そのままでよい
- 入れ子の中の値を、Excelで集計したい → 平らにしてから変換
配列(リスト)の入れ子
1つの項目に、複数の値(リスト)が入っている場合も、1つのセルに文字として入ります。リストの要素ごとに、行を分けたいときは、先に整形します。
向いている場面
- 構造が単純なJSONの変換(入れ子がない)
- 入れ子の中身は、そのまま確認できればよい場合
例
注文データのJSONを変換した。「商品」の項目に、商品の一覧が入っていて、1つのセルに文字として入った。商品ごとに集計するため、先に商品ごとの行になるよう整形してから、変換し直した。
つまずきやすい点
- セルに、かっこや記号だらけの文字が入る:入れ子の部分。
- 列の数が行によって違う:項目が揃っていないデータ。
よくある質問
入れ子を自動で展開できますか?
このツールは、入れ子の展開はしません。先に整形してください。
まとめ
入れ子は、1つのセルに文字として入る。集計したいなら、先に平らにしましょう。
CSVとJSONを相互変換するを使う(無料)あわせて使えるツール
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
- ファイルをZIPにまとめる:複数のファイルを、1つのZIPにまとめます。
- 文字数カウント(エントリーシート・レポート用):文章の文字数・行数・原稿用紙の枚数などを数えます。
同じツールの、ほかの解説
Notes on Nested JSON
- Paste
- Check
JSON from APIs and the like often has a "nested" form, where data contains further data. Here are cautions for converting nested JSON to CSV with Convert CSV and JSON.
Steps
- For "Direction", choose "JSON → CSV".
- Paste the JSON into "Before conversion".
- Press "Convert".
- Check "After conversion".
What nesting is
For example, data like this.
- Name: Yamada
- Address: (prefecture: Tokyo, city: Shinjuku)
Inside "Address" are further items. That is nesting.
The result after conversion
CSV is a flat table. Nested parts cannot take a table form, so their content goes into a single cell as text.
Ways to flatten
- Expand the items: bring the contents of the address out as separate items "prefecture" and "city".
- Fix with a text editor: change the structure with find-and-replace and so on.
- Extract just the needed parts: make JSON with only the target items.
How much to fix
- The values inside the nesting aren't needed → fine as it is
- You want to total the values inside the nesting in Excel → flatten before converting
Nested arrays (lists)
When one item holds several values (a list), it also goes into one cell as text. If you want rows split per list element, reshape first.
Good for
- Converting simply structured JSON (no nesting)
- When it is enough to see the nested contents as they are
Example
Converted order data JSON. The "products" item held a list of products and went into one cell as text. To total per product, reshaped it to one row per product first and converted again.
Common stumbling blocks
- A cell is full of brackets and symbols: that is the nested part.
- The number of columns differs by row: data with uneven items.
Frequently asked questions
Can nesting be expanded automatically?
This tool does not expand nesting. Reshape first.
Summary
Nesting goes into one cell as text. If you want to total it, flatten first.
Use "Convert CSV and JSON" (free)Tools that go well with this
- Remove Duplicate Lines and Sort a List: Remove duplicate lines and sort them.
- Make a ZIP File: Pack several files into one ZIP.
- Character Counter (for Applications and Reports): Count characters, lines and manuscript sheets of your text.