How to Convert CSV to JSON Without Losing Data
Published on • Author: Radek Grzybowski
CSV is convenient for spreadsheet exports. JSON is useful when an application needs named fields and structured values. Moving between them looks simple until a quoted field or a customer identifier changes unexpectedly. A reliable conversion starts with understanding the source and ends with checking a few representative records.
Inspect the CSV before converting
Open a copy in a plain text editor. Check whether the separator is a comma or another character such as a semicolon. Confirm whether the first record contains field names. A spreadsheet preview can hide quoting and line breaks that matter to a parser.
Quoted CSV fields can contain separators and line breaks. Quotes inside a field are commonly represented by a doubled quote. This is why splitting every line at each separator is unreliable. Use a CSV parser that reads full records and respects the chosen quote character.
Reference: RFC 4180 CSV format documentation
Decide which values should stay as text
Use unique headers when you want JSON objects with named fields. Two columns with the same name cannot be represented reliably as two properties in one object. Give them distinct names before conversion. Make sure each record has the expected number of fields.
An identifier is not necessarily a number. A value such as 00124 may be a product code whose leading zeros matter. Phone numbers and account references should usually stay as strings. Disable dynamic typing when the receiving system expects every field to be text.
Our converter preserves values with leading zeros and unsafe large integers as text. Even so you should review the result against the receiving system. A literal true or null can have a different meaning when converted to a JSON boolean or null value.
Use settings that match the source
Open CSV to JSON and paste your CSV. Set the delimiter and quote character. Enable headers if the first record contains field names. Check the comment setting. Disable comments when the chosen marker could be the first character of a real data record.
Review empty fields explicitly. In this converter an empty field remains an empty string. A short record receives empty strings for missing trailing fields. Extra fields produce an error instead of disappearing. Fix the source record when that error appears rather than guessing which column to remove.
Check structure as well as valid syntax
JSON requires quoted property names and does not allow trailing separators. Valid syntax is only the first check. Count the output records and compare a row with an empty field. Then check a row with quoted text and a row with an identifier. Confirm that the expected property names appear on each object.
Test a small sample in the receiving application before importing the full export. A system may require numbers or a specific date format even when your JSON is syntactically correct. Keep the original CSV and record your conversion settings so that the same export can be reproduced later.
Reference: RFC 8259 JSON specification