How to Convert JSON to CSV Without Breaking Your Data

JSON and CSV are two of the most common ways to move tabular data between tools. JSON is flexible and good for apps. CSV is simple and good for spreadsheets. Converting from JSON to CSV sounds easy: take the data, put commas between values, and save it. In practice, small details break the result. Commas inside values split a column in two. Line breaks inside a value create extra rows. Missing fields shift columns. This guide shows a safe, step-by-step method that preserves your data, plus a worked example you can check in a spreadsheet.

The key idea is that CSV is not just text with commas. It has rules for quoting, escaping, and headers. If you follow those rules, your data survives the trip. If you ignore them, you get a file that looks right at a glance but is wrong when you open it in Excel or Google Sheets.

How to Convert JSON to CSV

Step one is to look at your JSON and identify its shape. The easiest shape to convert is an array of objects, where each object is one row and each key is one column. For example, a list of people where each person has a name, a city, and an age is an array of objects. That shape maps cleanly to a table.

Step two is to collect the headers. Headers are the column names. Take the union of all keys across all objects, in a sensible order. If the first object has name and city but the second object also has age, the headers should include name, city, and age. Do not take headers from only the first object, or later objects will lose data.

Step three is to write one row per object, in the same column order as the headers. If an object is missing a key, leave that cell empty. Do not shift the later values left to fill the gap. An empty cell preserves alignment. A shifted row breaks it.

Step four is to handle special characters. If a value contains a comma, wrap the whole value in double quotes. If a value contains a double quote, double the quote and wrap the value in quotes. If a value contains a line break, wrap it in quotes. If a value is a number, leave it unquoted unless it must preserve leading zeros, such as a postcode or an ID.

Step five is to save the file as UTF-8, with a .csv extension, and with one header row at the top. Step six is to open it in a spreadsheet and check it, which we cover in detail below. Never assume the file is correct because it opened without an error. Open it, count rows and columns, and spot-check values.

Arrays of Objects and Headers

An array of objects looks like a list inside square brackets, where each item inside curly braces is one record. Each record has keys and values. For example, a record might have name: Asha, city: Mumbai, and age: 28. Another record might have name: Ravi, city: Delhi, and age: 34. Those two records share the same keys, so the headers are name, city, and age. That is the ideal case.

Real data is messier. One record may have an extra key, such as email, that others lack. Another record may have a nested object, such as an address with street and city inside it, or a list, such as a list of tags. CSV is flat, so nested data needs a decision. The simplest options are to flatten nested objects into separate columns, such as address_street and address_city, or to keep nested data as a JSON string in one column. Flattening is better for spreadsheets. Keeping it as a JSON string is better if you need to convert back to JSON later. Either way, decide once and apply it to every row.

Dates and numbers also need a decision. Keep dates in one consistent format, such as YYYY-MM-DD, so a spreadsheet does not misread them. Keep numbers as numbers, not as text with currency symbols, if you want to sort or sum them. If a number is actually an identifier, such as a phone number or a product code, keep it as text and preserve leading zeros.

Commas, Quotes, and Line Breaks

Commas in values are the most common cause of broken CSV files. Suppose a city value is New Delhi, India, or a name value is Patel, Asha. If you write that value without quotes, the comma looks like a column separator. The row gains an extra column, and every value after it shifts. The fix is to wrap the value in double quotes: “Patel, Asha”. The spreadsheet then treats the comma as part of the value, not as a separator.

Quotes inside values need a second rule. In CSV, a double quote inside a quoted value is written as two double quotes. For example, the value She said “hello” becomes “She said “”hello”””. That looks odd, but it is the standard rule. Most converters and spreadsheet apps handle it correctly if you follow it. If you use a single backslash or a single quote to escape, some apps will misread it.

Line breaks inside values are similar. A value that contains a paragraph or an address with a line break must be quoted, or the line break will look like a new row. If you can, replace line breaks with a space or a semicolon before converting. If you must keep them, quote the value and check that your spreadsheet shows the break inside one cell.

Worked Example: Asha and Ravi, Two Rows

Here is a complete worked example with two rows. The JSON is an array of two objects:

[{“name”: “Asha”, “city”: “Mumbai”, “age”: 28}, {“name”: “Ravi”, “city”: “Delhi”, “age”: 34}]

To convert it, first list the headers. The keys are name, city, and age. The CSV header row is therefore:

name,city,age

Next, write one row per person, in the same order. The first row is Asha, Mumbai, 28. The second row is Ravi, Delhi, 34. The full CSV is:

name,city,age
Asha,Mumbai,28
Ravi,Delhi,34

That is a clean conversion. Every row has three values. The headers match the values. No quoting is needed because none of the values contain a comma, a quote, or a line break.

Now let us make it more realistic. Suppose Asha lives in Mumbai, India, with a comma in the city description, and Ravi has a note that says Likes “data” tools. The JSON becomes harder, and the CSV must use quotes. The CSV header becomes name, city, age, note. Asha row becomes Asha, quoted Mumbai, India, 28, empty note. Ravi row becomes Ravi, Delhi, 34, quoted Likes doubled-quote data doubled-quote tools. The point is that quoting keeps the columns aligned even when values contain commas and quotes. If you skipped the quotes, the Mumbai row would split into four columns and the note row would break.

How to Check in a Spreadsheet

Checking is not optional. Open the CSV in Excel, Google Sheets, or LibreOffice Calc. First, count the rows. You should have one header row plus one row per JSON object. In the example above, that is three rows in total. If you have more, a line break or a comma may have created extra rows or columns.

Next, count the columns. Every row should have the same number of columns as the header row. In the example, that is three columns, or four if you added a note column. If one row has more columns, a comma was not quoted. If one row has fewer, a value was lost or a line break split the row.

Then spot-check values. Pick a few rows and compare them to the original JSON. Check a value with a comma, a value with a quote, a value with a line break, and a row with a missing field. Make sure empty cells are in the right columns. Check dates, numbers, and leading zeros. If a postcode like 00123 became 123, the spreadsheet treated it as a number. You may need to format that column as text or add a leading apostrophe in the CSV.

Finally, check encoding. If names with accents or non-English characters appear as strange symbols, the file was not saved as UTF-8 or the spreadsheet guessed the wrong encoding. Re-save as UTF-8 and re-open, or use the spreadsheet import wizard to choose UTF-8 explicitly. In Excel, use Data, then From Text or CSV, and choose UTF-8.

Common Tools and Methods

You can convert JSON to CSV in several ways. A dedicated converter website can work for small, non-sensitive data, but do not paste private or personal data into a random website. A spreadsheet app can import JSON in some versions, but the steps vary. A small script in Python, JavaScript, or another language gives you the most control, because you can define headers, quoting, and flattening exactly. If you use a script, use a well-tested CSV library rather than joining values with commas by hand. The library will handle quoting and escaping for you.

Whichever method you use, keep a copy of the original JSON. Do not delete it after conversion. If the CSV turns out to be wrong, you can convert again from the original. If you only keep the CSV, you may have lost nested data, types, or precision that cannot be recovered.

Limitations

CSV is a flat format. It cannot represent nested objects, arrays, or complex types as naturally as JSON. Flattening loses structure. Converting a list inside a record to a single string loses the ability to treat each item separately. Converting numbers to text loses the ability to calculate. If your data is deeply nested, CSV may not be the right format. Consider keeping it as JSON, or using a database or a spreadsheet with multiple sheets.

CSV also has no standard for types, dates, or encoding. One app may read 01/02/2026 as 1 February, another as 2 January. One app may treat a long number as text, another may round it. A CSV file does not carry the information needed to resolve those choices. You must agree on formats before sharing the file, and you must check the result in the app that will use it.

Finally, CSV files do not have built-in validation. A missing comma, an extra quote, or a stray line break can break the whole file silently. That is why checking in a spreadsheet, counting rows and columns, and spot-checking values matter more than the conversion itself.

Frequently Asked Questions

Why does my CSV look broken in Excel?

The most common reasons are unquoted commas, incorrect encoding, or a delimiter mismatch. If your data uses semicolons instead of commas, or if Excel expects a different separator, the file will split in the wrong places. Check quoting, save as UTF-8, and use the import wizard to choose the correct delimiter.

How do I handle a missing field?

Leave the cell empty and keep the other values in their correct columns. Do not shift values left. An empty cell preserves alignment. If many rows are missing the same field, consider whether that column is needed at all.

What if a value contains a comma?

Wrap the whole value in double quotes. For example, “Mumbai, India”. If the value also contains a double quote, double the quote and keep the outer quotes. Use a CSV library to handle this automatically rather than doing it by hand.

Can I convert CSV back to JSON?

Yes, if the CSV is clean and the headers are clear. Each row becomes one object, and each header becomes a key. You may need to convert numbers and dates back to the right types, because CSV stores everything as text. Keep the original JSON if you need exact types.

Converting JSON to CSV is safe when you respect the format. Start with an array of objects, choose headers from all keys, keep one row per object, quote values that contain commas, quotes, or line breaks, and check the result in a spreadsheet. In the worked example, Asha and Ravi became two clean rows under name, city, and age. With those checks, your data will arrive intact.