Home / Blog / Transliteration & conversion

Spreadsheet formats & CSV

How Convert JSON To CSV?

A JSON-to-CSV conversion works when you define a rectangular table from the JSON structure.

How Convert JSON To CSV?

A JSON-to-CSV conversion works when you define a rectangular table from the JSON structure. For a flat array of objects, choose a fixed field list and serialize each object as one row. The following Python example makes missing and extra fields explicit errors.

Set up the conversion

Load JSON using the standard library, verify that its top-level value is a list, and check every item against the expected fields. The example deliberately rejects unknown fields so an updated input schema cannot silently lose data. It also rejects nested structures, which otherwise turn into language-specific strings instead of a useful interchange format.

A reproducible conversion example

import csv, json

with open("input.json", encoding="utf-8") as source:
    rows = json.load(source)
fields = ["id", "name", "note"]
if not isinstance(rows, list):
    raise ValueError("Expected an array of objects")
for row in rows:
    if not isinstance(row, dict) or set(row) != set(fields):
        raise ValueError("Each record must contain id, name and note")
    if any(isinstance(value, (dict, list)) for value in row.values()):
        raise ValueError("Nested values need an explicit mapping")
with open("output.csv", "w", encoding="utf-8", newline="") as target:
    writer = csv.DictWriter(target, fieldnames=fields, extrasaction="raise")
    writer.writeheader()
    writer.writerows(rows)

The code is an illustrative conversion core. Run it in an isolated working folder with the named test files, install only the dependencies used by the example, and handle its reported errors before applying it to a larger dataset. It does not contact a remote conversion service.

Check the expected result

Use two records with different values, including a comma and quotation marks in note. Parse output.csv with DictReader and compare field order and record count. JSON null is written as an empty field by the CSV writer; if your recipient needs to distinguish null from an empty string, define a sentinel or separate status field before export. Large numeric identifiers should originate as strings when exact decimal digits matter.

Format-specific limits

CSV has no native nested values or type metadata. Flattening address.city can be reasonable, but exploding an orders array changes one record into many rows. Those are different contracts and should be tested separately. Do not select an arbitrary first nested value simply to make the file rectangular.

Verify the file contract before using the result

Write down the delimiter, encoding, header names, field order and treatment of empty values. Keep the original file unchanged and write the converted output to a different path. Conversion is successful when a parser for the destination format recovers the intended values; a familiar filename extension or a successful save message is not enough.

Start with a small fixture that contains the cases your real data can contain. Include a leading-zero identifier, a quoted comma, an embedded quotation mark, a blank field and a non-ASCII character. Add a multiline field only if both formats and the receiving system support it. Verify the fixture first, then run a bounded sample from the real input. Record parsed row counts and check the first and last records so truncation or an accidental duplicate header is visible.

Keep identifiers separate from quantities. An account-like or product identifier may contain digits while still being text; converting it to a floating-point number can remove leading zeros or precision. Dates also need an agreed format. When a conversion produces an unexpected blank, investigate whether it came from an empty source cell, a missing field, an invalid value or an automatic missing-value rule. Those conditions can require different downstream handling.

Do not resolve malformed records by silently skipping them unless that behavior is an explicit requirement and the skipped records are reported. Use a failure message that identifies the record or field, retain enough context to correct the input, and avoid exposing sensitive contents in public logs. For production automation, write to a temporary destination and promote it only after validation so a failed run cannot replace a previously good export.

Where the browser tool fits

Convert Text to CSV helps with already extracted plain text. It does not replace a workbook reader, a JSON parser, an XML schema validator or a Parquet engine. Use it only when its input structure and delimiter choices match your actual data. For structured source files, perform the format-aware conversion above and check the result with an independent reader.

Primary reference

Official format and API documentation