1行目を見出しにするかどうか
- 確かめる
- 選ぶ
CSVをJSONに変換するときの大きな分かれ道が、「1行目を見出しとして使う」にチェックを入れるかどうかです。CSVとJSONを相互変換する の画面で、その違いを説明します。
手順
- 「変換の向き」で、「CSV → JSON」を選びます。
- 「変換前」に、CSVを貼り付けます。
- 「1行目を見出しとして使う(CSV → JSON)」の、チェックを決めます。
- 「変換する」を押して、「変換後」を確認します。
チェックありの場合
1行目が項目名になり、2行目以降が、その項目の値になります。
CSV:
名前,年齢
山田,30
結果:「名前: 山田」「年齢: 30」のように、項目名のついたデータになります。
プログラムで使うときは、項目名で取り出せるので、扱いやすい形です。
チェックなしの場合
すべての行が、同じ扱いになり、行ごとの値の並び(配列)になります。
結果:「山田, 30」のように、項目名のない値の並びになります。
どちらを選ぶか
- 1行目に項目名がある表 → チェックあり
- 項目名がない、数値の並びだけのデータ → チェックなし
- 迷ったら → チェックありにして、結果を確認する
見出しがない表にチェックを入れた場合
1行目のデータが、項目名になってしまい、最初の行のデータが失われます。結果を見て、おかしければ、チェックを外します。
見出しが同じ名前の場合
同じ項目名が複数あると、後ろの値が、前の値を上書きしてしまう場合があります。見出しは、重複しないように直します。
向いている場面
- プログラムで、項目名を指定してデータを取り出したい(チェックあり)
- グラフ用の、数値の並びを作りたい(チェックなし)
例
商品名・価格・在庫の表を、WebアプリのデータにするためにJSONへ変換した。1行目が見出しなので、チェックを入れた。
つまずきやすい点
- 見出しの前に、タイトル行がある:先に、タイトル行を削除する。
- 日本語の見出し:そのまま項目名になる。プログラム側で、扱えるか確認する。
よくある質問
後から、チェックを変えられますか?
できます。チェックを変えて、もう一度「変換する」を押します。
まとめ
1行目が項目名ならチェックあり。結果を見て、おかしければ切り替えましょう。
CSVとJSONを相互変換するを使う(無料)あわせて使えるツール
- 重複した行を消す・並べ替える:重複した行を消して、並べ替えます。
- ファイルをZIPにまとめる:複数のファイルを、1つのZIPにまとめます。
- 文字数カウント(エントリーシート・レポート用):文章の文字数・行数・原稿用紙の枚数などを数えます。
同じツールの、ほかの解説
Using the First Row as Headers
- Check
- Choose
The big fork in converting CSV to JSON is whether to tick "Use the first row as headings". Here is the difference, following the screen of Convert CSV and JSON.
Steps
- For "Direction", choose "CSV → JSON".
- Paste the CSV into "Before conversion".
- Decide the tick for "Use the first row as headings (CSV → JSON)".
- Press "Convert" and check "After conversion".
With the tick
The first row becomes item names, and the second row onwards become the values of those items.
CSV:
name,age
Yamada,30
Result: data with item names, like "name: Yamada" and "age: 30".
When used in a program, values can be retrieved by item name, an easy-to-handle form.
Without the tick
All rows are treated alike and become a sequence (array) of values per row.
Result: a sequence of values without item names, like "Yamada, 30".
Which to choose
- A table with item names in the first row → tick
- Data with no item names, just a sequence of numbers → no tick
- If unsure → tick it and check the result
If you tick for a table without headings
The first row of data becomes the item names and the first row's data is lost. If the result looks wrong, untick.
When headings have the same name
If the same item name appears more than once, later values may overwrite earlier ones. Fix headings so they don't repeat.
Good for
- Wanting to retrieve data in a program by specifying item names (tick)
- Making a sequence of numbers for a chart (no tick)
Example
Converted a table of product name, price and stock to JSON to use as web app data. The first row was headings, so ticked.
Common stumbling blocks
- A title row sits above the headings: delete the title row first.
- Japanese headings: they become item names as they are. Check that the program side can handle them.
Frequently asked questions
Can I change the tick afterwards?
Yes. Change the tick and press "Convert" again.
Summary
If the first row is item names, tick. Look at the result and switch if it looks wrong.
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.