How to Convert JSON to CSV Without Losing Data?

A CSV file is a simple table saved as text. Each line is a row, and commas split the columns. Turning a flat list of JSON records into CSV is easy.

But real data from an API is often not flat. It has objects inside objects and lists of tags. Some fields are only in some records. And spreadsheet apps like Excel sometimes change your values. This guide shows how to handle each problem.

Quick answer: Turn nested objects into columns with dot names, like address.city. Build the header from the keys of every record, not just the first one. Put quotes around values that have commas, quotes or line breaks. If a person will open the file in Excel, make a real .xlsx file so IDs and leading zeros stay correct.

Nested objects: turn them into columns

CSV is a flat grid. So objects inside objects must become columns. The common way is to join the key names with a dot:

{ "id": 1, "address": { "city": "London", "country": "UK" } }

id,address.city,address.country
1,London,UK

This keeps every value, and you can still see the structure. Later, you can turn the dot names back into nested JSON. This step is called flattening.

The Flatten JSON tool shows you what the keys will look like. You can also pick a different separator instead of the dot.

Arrays: choose one way to handle them

There is no single right way to put a list (an array) into one CSV row. Here are the three common choices:

  • One column per item — tags.0, tags.1… This works well for short lists that always have the same length. It is hard to use when the lengths change.
  • JSON text in one cell — ["math","poetry"]. Nothing is lost, and it is small. But the program that gets the file has to read the JSON text.
  • One row per item — this is also called “exploding” the array. You repeat the other fields on every row. This is best for looking at the data in a spreadsheet or a report tool. But you get more rows than records.

Choose one way on purpose, and write down which one you used. The person who imports the file needs to know.

Missing keys: include every column

Sometimes only some records have a field. The header must still include that field. The other rows just get an empty cell.

Some converters build the header from the first record only. They drop later fields without any warning. So check your columns. The number of columns should match the number of different keys across all records.

Quotes: follow RFC 4180

RFC 4180 is the common standard for CSV files. It has a few rules for values:

  • If a value has a comma, a double quote or a line break, put double quotes around it.
  • If a value has a double quote inside, write that quote twice.

For example: "Coined ""debugging""".

Many people write their own code with join(','). This is the most common reason a CSV file opens with its columns in the wrong place.

Spreadsheet problems to watch for

Leading zeros

ZIP codes, phone numbers and product codes can start with zero, like 02134. When Excel opens a CSV, it guesses this column holds numbers. So it removes the leading zero.

You can fix this in two ways. Import the CSV with Excel’s data import tool and set the column type to Text. Or skip CSV and make a real .xlsx file that stores the cell as text.

Long numbers

Excel keeps only the first 15 digits of a number. So a 19-digit order ID turns into 1.23457E+18. The last digits become zeros. Once you save the file, those digits are gone for good.

Store long IDs like this as text. JavaScript has a similar problem when it reads JSON. See why JSON.parse changes big numbers.

Commas, semicolons and your region

In many European countries, the comma is used in decimal numbers, like 3,5. So Excel there expects a semicolon between columns, not a comma.

If your CSV opens with everything in column A, export it again. This time, use a semicolon as the separator.

Accents and emoji

UTF-8 is the most common way to store text with accents and emoji. But Excel does not always notice that a CSV file uses UTF-8. Then “café” can show up as “café”.

To fix it, import the file from the Data tab and choose UTF-8. Or use an .xlsx file, which does not have this problem.

CSV or Excel?

Use CSV when another program will read the file. For example, a database import, a data pipeline or a script.

Use a real Excel file when a person will open it. Excel files keep the type of each cell. They have no problems with text encoding or separators. And long IDs stay exact.

On JSONPilot, JSON to CSV turns nested objects into columns. It builds the header from every record, and it quotes values the right way.

JSON to Excel makes a real .xlsx file. It stores IDs longer than 15 digits as text. Both tools run in your browser, so your data is never uploaded.

Common questions

Can CSV hold nested JSON?

Not directly, because CSV is a flat grid. You have to turn nested objects into columns with dot names. Or you can put the nested part as JSON text in one cell.

Why are some of my fields missing from the CSV?

Your converter probably built the header from the first record only. Fields that appear only in later records get lost. Use a tool that reads the keys from every record.

Why does Excel remove the zero at the start of my numbers?

Excel thinks the column holds numbers, and numbers don’t start with zero. Import the column as Text, or use a real .xlsx file that stores the value as text.

Why does my long ID look like 1.23457E+18 in Excel?

Excel keeps only the first 15 digits of a number, so it rounds longer numbers. Store long IDs as text to keep every digit.