
Converting JSON to CSV: The Edge Cases That Will Ruin Your Export
I was asked to export a user database as a CSV for the finance team. Simple enough: JSON in, CSV out. The script took twenty minutes to write and three days to debug. The problems were not with the conversion logic -- they were with the data. Commas in address fields. Newlines in description fields. Nested objects that had no obvious flat representation. Unicode characters that Excel mangled on import. Every one of these is a known problem, and every one has a known solution. What CSV actually is CSV (Comma-Separated Values) is deceptively simple. Each line is a record. Each field is separated by a comma. The first line is optionally a header row. name,email,city Michael,mike@example.com,New York Jane,jane@example.com,San Francisco That is the entire format for simple data. The complexity comes from the escaping rules, which are defined in RFC 4180: If a field contains a comma, newline, or double quote, the entire field must be enclosed in double quotes. name,address,note Michael,"123
Continue reading on Dev.to JavaScript
Opens in a new tab

