A JSON card with an arrow to a spreadsheet grid

How to export JSON to CSV without losing meaning

A CSV export is useful for a flat table, but it cannot represent every JSON structure without a deliberate flattening rule. JSON to CSV conversion looks like a mechanical task: feed in an array of objects, get out a spreadsheet. In practice, it is a translation between two different data models, and every translation decision can change the meaning of the data. Nested objects, arrays, missing fields, numbers that must stay text, and values that look like spreadsheet formulas all need a rule before you convert, not after.

This guide gives you a repeatable method: confirm the shape of your input, choose a flattening rule, fix the column list, handle types and missing values consistently, and verify the output before you share it. It also works through a concrete example so you can see exactly where meaning is preserved and where it is deliberately simplified.

Why JSON to CSV loses meaning so easily

JSON can represent nested objects, arrays, typed numbers, booleans, and null. CSV can represent a flat table of text fields. When you convert, you must decide what happens to everything CSV cannot express. That decision is where meaning is lost.

Consider a JSON record for a customer with an address object, a list of tags, and an orders array. CSV has no address object, no list, and no nested array. You must either flatten the address into columns like address_city and address_postcode, join the tags into a single string, stringify the orders array into one cell, split the record across multiple rows, or drop something. Each choice produces a different CSV file, and a reader who does not know which choice you made will misinterpret the result.

Types are the second source of loss. JSON distinguishes the number 42 from the string 42, the boolean true from the string true, and null from an empty string. In CSV, all of those become text. A downstream reader must guess the type from the text, and different readers guess differently. A product code of 00123 can become the number 123. A large identifier can become scientific notation. A null can become an empty cell that a reader treats as zero, as missing, or as an empty string.

The third source of loss is shape variation. JSON records in the same array do not have to share the same keys. One record may have a discount field that others lack. One record may have an address object while another has an address string. A CSV writer must pick one column set for all rows, and any key that does not fit the chosen columns is either given an empty cell or dropped entirely. If the writer only looks at the first record to build the column list, keys that appear later are silently lost.

Step 1: Confirm the input shape

Before converting anything, confirm that the input is an array of records, and inspect what those records actually contain. Do not assume that every record has the same keys, the same nesting, or the same types for the same key.

  1. Is the top level an array? A JSON to CSV converter expects a list of objects. If your input is a single object, a nested object with the array buried inside it, or an array of arrays, extract the record list first. Converting the wrong level produces a CSV file with one row, one column, or a wall of stringified JSON.
  2. Are all records objects? If some entries are strings, numbers, or null, decide how to represent them. A common rule is to reject mixed input rather than silently converting non-objects into rows with a singl

    Step 5: Quoting, delimiters, and encoding

    CSV quoting is where many exports break silently. Any field that contains a comma, a double quote, or a line break must be quoted, and a double quote inside a quoted field is escaped by doubling it. A converter that follows RFC 4180 handles this correctly. A converter that simply joins fields with

    Limitations

    Even a careful export has limits. CSV cannot preserve JSON types, nesting, or the distinction between null and an empty string without extra conventions, and any convention you add is only meaningful if the consumer knows about it. A flattened export is a view of the data, not a copy of it. If the original JSON is the authoritative record, keep it, and treat the CSV file as a derived report rather than a replacement.

    There are also practical limits. A browser tool is a p

    Step 4: Types, numbers, dates, and booleans

    ractical aid for small examples and inspection; it is not a substitute for a controlled pipeline for large or sensitive datasets. Large exports need streaming, validation, and access control. Sensitive exports need the same care as any other handling of personal or confidential data: minimise what you export, check who will receive the file, and do not paste sensitive records into a browser tool or share them through an unsecured channel. If the export contains personal data, confirm that the export is permitted and that the recipient is entitled to receive it.

    Finally, a CSV export is only as good as the decisions behind it. If the flattening rule is undocumented, if the column list changes between exports without notice, or if verification is skipped, the file will mislead someone eventually. The method in this guide is deliberately boring: confirm, decide, document, verify. That is what makes it reliable.

    Frequently asked questions

    Why does my JSON to CSV export lose some fields?

    The most common cause is that the converter built its column list from the first record only, and later records contained keys the first record lacked. Fix it by scanning all records to build the union of keys before converting, and by checking the output row by row against the input. A second cause is nested values that the converter dropped instead of flattening or stringifying. Decide a flattening rule for every nested field before you convert, and verify that each nested value appears in the output in some form.

    How do I keep leading zeros when exporting to CSV?

    Keep the value as text throughout, and make sure the consumer imports it as text. The CSV file itself can hold 00123 correctly, because CSV is plain text. The loss usually happens when a spreadsheet opens the file and converts the column to a number. Import the file explicitly as text, or warn the recipient to do so, and verify the result by inspecting the raw CSV file in a text editor rather than trusting the spreadsheet view.

    Should nested objects be flattened or stringified?

    It depends on the consumer. Flatten with dot notation when the reader needs to filter, sort, or analyse the nested values in a spreadsheet. Stringify as JSON when the reader needs to recover the full nested value exactly, for example when another program will parse it back. Join arrays into a readable string only when the reader needs human-readable text and will not parse the values back out. Document whichever choice you make.

    Is it safe to open a CSV export in a spreadsheet?

    Only after checking for formula-like values. Cells that begin with =, +, -, or @ can be interpreted as formulas by spreadsheet programs, and crafted values can be used for formula injection attacks. Inspect the data for such values before sharingCSV has no types, so you must protect
    the file, neutralise them in a documented way, and prefer importing the file as text rather than double-clicking it open. If the file is destined for a program rather than a spreadsheet, say so, and do not rely on spreadsheet behaviour.

    Use the related tool carefully

    This guide is paired with a browser-only utility. The tool is a practical aid for small examples and inspection; it is not a substitute for application testing, a formal data pipeline, or professional review where the decision has higher consequences.

    Open the related JSON to CSV Converter

    Related tools in this category

    Sources

    commas will produce a file where one field spills into neighbouring columns and every later value shifts.

    Confirm the delimiter and encoding before you share the file. If the data contains commas as decimal separators, a semicolon-delimited file may be more appropriate for the consumer’s locale, but only if the consumer expects it. Write the file in UTF-8, and say so in the export notes, so that accented characters and non-Latin scripts survive the transfer. If the consumer’s tool expects a byte-order mark, add one deliberately rather than letting it appear by accident. And remember that a field can legitimately contain a line break: a single CSV record can span multiple physical lines, so a tool that counts lines is not counting records.

    Step 6: Formula safety before opening in a spreadsheet

    Review formula-like values before opening a CSV in a spreadsheet program. A data export should not accidentally become executable spreadsheet content. This is a real security issue, not a formatting quirk.

    Spreadsheet programs treat a cell that begins with =, +, -, or @ as a formula. If your JSON data contains a value such as =2+5, a user id that starts with +, or a text field that begins with @, a spreadsheet may interpret it as a formula when the CSV file is opened. In the worst case, a crafted value can execute a formula that exfiltrates data or runs a command when the file is opened. This class of issue is often called CSV injection or formula injection.

    Mitigate it at export time. Inspect the data for values that begin with =, +, -, @, tab, or carriage return, including values with Step 4: Types, numbers, dates, and booleans
    leading whitespace before those characters. Neutralise them in a documented way, for example by prefixing the value with a single quote or a space, or by prepending a tab, depending on what the consumer expects. If the consumer is a program rather than a spreadsheet, the safest option is to leave the values untouched and warn the consumer to import the file as text rather than opening it directly. Whatever you choose, do not leave formula-like values unexamined in a file that people will double-click.

    Step 7: Verify the output before sharing

    Verification is not optional. A converter that reports no error may still have dropped data, shifted columns, or reformatted values. Check the output against the input with this short checklist:

    • Row count: does the number of data rows match what you expect under the flattening rule you chose? If you flattened one row per array item, the row count will be higher than the number of JSON records, and you should be able to explain why.
    • Column count: does every row have the same number of fields as the header? A row with fewer or more fields is a sign of a quoting or flattening failure.
    • Spot-check values: pick three records, including one with nesting, one with missing fields, and one with unusual characters, and compare the CSV row against the JSON record field by field.
    • Round-trip test: convert the CSV back to JSON with a trusted tool and compare it with the original. The round trip will not be identical, because types and nesting were simplified, but every value that should have survived should be present and correct.
    • Raw text inspection: open the CSV file in a text editor, not just a spreadsheet, and confirm that quoting, delimiters, and encoding look right. A spreadsheet view hides exactly the details that matter.

    If any check fails, fix the export rule and re-export. Do not patch the CSV file by hand and ship it, because the next export will contain the same bug.

    Worked example: exporting a small order list

    Suppose your JSON is an array of two orders. Each order has an id, a customer name, an address object with a city and a postcode, a tags array, and a total. Here is how the method above applies.

    First, confirm the shape. The top level is an array. Every entry is an object. Scanning all records shows the same keys: id, customer, address, tags, total. The address field is an object in every record, and tags is an array in every record. Types are consistent: id is a string, total is a number, tags is an array of strings.

    Second, choose the flattening rule. For this export, the consumer is a colleague who will filter and sort in a spreadsheet, so you choose dot notation for the address and a joined string for tags. The address object becomes two columns, address.city and address.postcode. The tags array becomes one column, tags, with values joined by a semicolon and a space. You note both decisions in the export notes.

    Third, fix the column list and order: id, customer, address.city, address.postcode, tags, total. You keep this order stable across every export, so a colleague who builds a report on column positions does not break when a new export arrives.

    Fourth, handle types and missing values. The id values look numeric but are identifiers, so you keep them as text and note that they must not be converted to numbers. The total values are numbers, so you write them without thousands separators and without a currency symbol. If one order had no tags, you would write an empty cell and note that empty means no tags, not missing data.

    Fifth, verify. You open the CSV file in a text editor and check that the header row matches the column list, that each row has the same number of fields, that the address values landed in the right columns, and that the tags cell for each row contains the expected values. You also check that no id lost a leading zero, that no total gained a thousands separator, and that no cell starts with a character a spreadsheet would treat as a formula.

    The result is a CSV file that a spreadsheet reader can use directly, with a clear statement of what was flattened and how. It is not a perfect copy of the JSON: the nesting is gone, and the types are now text. But nothing was silently dropped, and anyone who reads the export notes can reconstruct the original structure.

    Now consider what would have gone wrong with a naive export. A converter that only inspected the first record would have produced the same columns here, because the records happen to share keys. But if the second order had an extra discount field, that field would have been lost. A converter that stringified the address object would have produced a cell containing a JSON string, which a spreadsheet reader could not filter by city. A converter that joined tags with a comma would have broken the CSV structure, because the comma inside the field would need quoting, and a careless reader would split the cell. Each of those failures is avoidable once you treat flattening as a decision rather than a default.

    Step 3: Fix the column list and keep it stable

    Use a stable field order and keep the header row explicit. Missing fields should be represented consistently rather than silently shifting columns. Build the column list from the union of keys across all records, decide the order once, and use the same order for every export of the same dataset.

    When a record lacks a key, write an empty cell in that column. Do not omit the cell, or every later value in the row will shift left and land under the wrong header. Do not write a placeholder like N/A unless you document it, because a reader may treat N/A as a real value. And do not let a converter drop a column just because the first few records lack it.

    Also decide how to handle keys that appear with different types. If a field is a string in one record and a number in another, pick one representation and apply it to all rows, or split it into two columns with clear names. If a field is sometimes an object and sometimes a string, normalise it before converting rather than letting the converter stringify one and copy the other. Consistency across rows matters more than cleverness in any single row.

    Step 4: Types, numbers, dates, and booleans

    JSON feature In CSV Decide first
    Nested objects Flattened with a rule The flattening rule
    Arrays Joined, or one row per item One rule for all arrays
    Numbers Plain text, type lost How numbers are formatted
    Dates Plain text string The date format to use
    Booleans true/false text Lowercase or TRUE/FALSE
    Empty values Empty cell, means nothing on its own What empty means

    CSV has no types, so you must protect the values that depend on their type. The most common casualties are identifiers, large numbers, dates, and booleans.

    Identifiers: product codes, account numbers, and other identifiers that look numeric must stay text. Leading zeros matter, and a spreadsheet that converts 00123 to 123 has destroyed information. If the consumer will open the file in a spreadsheet, warn them, and consider adding a note that identifiers must be imported as text. For a programmatic consumer, document that the column is a string even though it looks numeric.

    Large numbers: identifiers and values beyond the precision of a double-precision float can lose precision when parsed as numbers. A 20-digit account number read as a float may come back with its last digits replaced by zeros. Keep such values as text throughout the export.

    Dates and times: write dates in an unambiguous format, preferably ISO 8601 (2026-10-16), and include the timezone for datetimes where it matters. A date written as 10/11/2026 is ambiguous between October 11 and November 10 depending on locale. A datetime without a timezone is ambiguous about which instant it refers to. Do not rely on the reader’s locale to resolve either question.

    Booleans and null: decide on one spelling for true and false, and one representation for null, and use them everywhere. Common choices are true and false for booleans, and an empty cell for null, with a note that empty means null rather than an empty string. If the distinction between null and an empty string matters for your data, say so explicitly, because CSV alone cannot express it.

    e value column.

  3. What keys exist across all records? Scan every record, not just the first one, and build the union of all keys. This is the only way to avoid losing columns that appear late in the file.
  4. Where is the nesting? List every field whose value is an object or an array. These are the fields that need a flattening rule. Everything else can usually become a plain column.
  5. Are types consistent? Check whether the same key is sometimes a string and sometimes a number, sometimes a single value and sometimes an array. Inconsistent types need a rule, not a guess.

A quick inspection pass like this takes a few minutes and prevents the most common class of export bug: a file that converts without an error but silently drops or distorts data.

Step 2: Choose a flattening rule and write it down

Flattening is the process of turning nested JSON into flat columns. Pick one rule per nested shape, apply it consistently, and document it alongside the export. The most common rules are:

  • Dot notation for objects: an address object with city and postcode becomes two columns, address.city and address.postcode. This preserves the values and keeps them queryable, and it is the best default for objects with a small, stable set of keys. It breaks down when objects have deeply nested or highly variable keys, because the column count can explode.
  • Stringified JSON for objects and arrays: a nested value is written as a JSON string inside a single cell, for example a tags array becomes the text [“new”,”sale”]. This preserves the full value in a recoverable form, but the cell is no longer convenient for filtering or sorting in a spreadsheet. A reader who does not know the cell contains JSON will treat it as opaque text.
  • Joined strings for arrays: a tags array becomes new; sale in one cell. This is readable and easy to filter by eye, but it is lossy: you cannot reliably split it back into the original array unless the separator can never appear inside a value, and the result is no longer typed. Use it only when the consumer needs human-readable text and will not parse the values back out.
  • One row per array item: a record with three line items becomes three rows, with the parent fields repeated on each row. This preserves all the detail and keeps each row flat, but it changes what a row means. The file no longer has one row per order. Anyone who counts rows, sums a total, or averages a per-order value without deduplicating will get a wrong answer.
  • Separate files: split the data into a parent file and a child file linked by an id. This is the most faithful option for deeply nested data, but it produces more than one file, and the consumer must join them correctly.

There is no universally correct choice. The right rule depends on who will read the CSV file and what they will do with it. A file destined for a spreadsheet reader who will filter and sort usually wants dot notation and joined strings. A file destined for another program that will parse values back into JSON usually wants stringified JSON. A file destined for a database import usually wants one row per item or separate files. Choose the rule for the actual consumer, and say which rule you used in the export notes.