Why splitting CSV on commas fails
In a CSV file, a comma inside a quoted field belongs to the value. A line break inside quotes can belong to it too.
Read the fields inside quotes
Count two product rows in the CSV below. The second row takes up two lines because its shipping note contains a line break.
product,note
"pen, blue",Single item
"Desk ""mini""","Ships flat
Handle with care"
"pen, blue"is one field. Its comma is part of the product name.- Two double quotes in a row inside a quoted field stand for one literal double quote. That turns
""mini""into"mini". - The line break after
Ships flatbelongs to the shipping note. The row continues until after the closing quote.
line.split(",") would cut pen, blue into two fields. Splitting at each line break would also cut the shipping note in two. A CSV parser follows the quoting rules in RFC 4180 to keep those values together.
Convert the full table to JSON
Paste the sample into the CSV-to-JSON converter and leave the header option on. The output has one object per product. The header names become its keys, and the line break appears as \n.
[
{"product": "pen, blue", "note": "Single item"},
{"product": "Desk \"mini\"", "note": "Ships flat\nHandle with care"}
]
To get just the product names, open the column converter and select product. For an export that uses pipes, choose Custom separator and enter |. A pipe inside a quoted field stays in the value.