CSV to JSON
Convert simple CSV rows into JSON objects using the first row as headers.
Written by Anil Gautam · Updated
How does CSV to JSON turn rows into objects?
The first line supplies the keys, and every later non-empty line becomes one object in a JSON array. Lines are split at every comma, and spaces around headers and values are trimmed. Take a header line name,city,score followed by the rows Asha,Pune,91 and Ravi,Delhi. The output holds two objects: Asha's has all three keys with score set to "91", and Ravi's has only name and city, since his line stops after two values. Indentation is two spaces.
Every value stays a string. Values such as true and null come out as the strings "true" and "null", and leading zeros in phone numbers and IDs such as 007 stay intact. Convert numbers in your own code if it expects them. A row with more values than headers gets extra keys named by position, such as col4 for a fourth value, and when two headers share a name, the later column overwrites the earlier one. Without a header line, your first record turns into the keys and drops out of the data.
Why does my CSV split into the wrong columns?
The splitter ignores quotes. In the line Asha,"Pune, MH",91 the comma inside the quotes is treated as a separator, so city becomes "Pune with a stray quote mark, score becomes MH" and 91 moves to a new key, col4. Excel and Google Sheets put quotes around any exported cell that contains a comma, so addresses and names written as Last, First are the usual cause.
Other separators fail without any warning. A semicolon-separated file, common in regions that write decimals with a comma, turns each whole line into one value under a single long key such as "a;b;c". Tab-separated text behaves the same way. If no value contains a comma, Find and Replace can change every ; to a comma before you convert.
How are line endings and blank lines handled?
Windows line endings (CRLF) are handled, so text copied from a CSV saved on Windows splits into the right rows. Empty lines are skipped, while a line holding only spaces becomes an object with an empty first value, so delete stray whitespace lines before you convert.
Is my CSV uploaded anywhere?
The conversion runs in your browser, so customer lists and exported records are not sent to a server. For the reverse trip, JSON to CSV builds a header from object keys. Remove Duplicate Lines can clear repeated rows before you convert, and JSON Minifier squeezes the output onto one line when you need to paste it into code.