Skip to content
iamnoobs

Developer Tutorials

How to Convert JSON to CSV

Convert a JSON array into CSV rows, understand the expected data shape, and avoid common conversion problems.

By iamnoobs

How to Convert JSON to CSV

JSON is widely used to exchange data between web applications, APIs, and backend services. However, when you need to analyze that data in a spreadsheet, prepare a report, or share records with someone who uses Excel, CSV can be a more convenient format.

Converting JSON to CSV transforms structured JSON data into rows and columns. The process is straightforward when your JSON contains an array of objects, but nested objects, arrays, missing properties, and special characters can require additional handling.

In this guide, you'll learn which JSON structures can be converted to CSV, how to convert JSON step by step, and how to troubleshoot common conversion issues. You can also use the IamNoobs JSON to CSV Converter to convert compatible JSON data.

When Can JSON Be Converted to CSV?

JSON and CSV represent data differently. JSON supports nested structures and multiple data types, while CSV is primarily designed for tabular data.

The easiest JSON structure to convert is an array of objects with consistent fields.

Example of JSON data

Consider the following JSON containing employee records:

[
  {
    "id": 101,
    "name": "Amit",
    "department": "Engineering",
    "salary": 60000
  },
  {
    "id": 102,
    "name": "Priya",
    "department": "Marketing",
    "salary": 55000
  },
  {
    "id": 103,
    "name": "Rahul",
    "department": "Support",
    "salary": 45000
  }
]

Each object represents one employee, and the properties represent the columns you want in the CSV file.

The corresponding CSV output is:

id,name,department,salary
101,Amit,Engineering,60000
102,Priya,Marketing,55000
103,Rahul,Support,45000

Each JSON object becomes a row, and the object keys become the column headers.

Which JSON structures convert well?

JSON structure Conversion suitability Explanation
Array of objects Excellent Each object can become a row.
Array of objects with missing fields Good Missing values can become empty cells.
Array of primitive values Possible Values can become a single column or rows, depending on the converter.
Single JSON object Possible Its properties can become one row or key-value pairs.
Nested objects Requires handling Properties may need to be flattened into columns.
Arrays inside objects Requires handling Arrays may need to be joined, serialized, or expanded.
Mixed or inconsistent structures Depends on the data A consistent column schema may need to be defined.

For most spreadsheet and reporting workflows, start with a JSON array containing objects with similar properties.

Why can't every JSON structure be converted directly?

JSON can represent data such as:

{
  "employee": {
    "name": "Amit",
    "department": "Engineering"
  },
  "skills": ["C", "Python", "Embedded Systems"]
}

This structure contains a nested object and an array. CSV has no native representation for either structure.

A converter must decide whether to flatten nested fields, serialize arrays as JSON strings, join array elements, or create multiple rows. Different choices produce different CSV layouts.

Tip: Before converting complex JSON, decide how nested information should appear in your spreadsheet.

Convert JSON to CSV Step by Step

You can convert JSON to CSV using an online converter, a Python script, or a JavaScript application.

Method 1: Use an online JSON to CSV converter

An online converter is useful when you need to convert data quickly without installing software or writing code.

Follow these steps:

  1. Open the IamNoobs JSON to CSV Converter.
  2. Paste your JSON data into the input field or upload a JSON file if file uploads are supported.
  3. Verify that the JSON is valid and that the data structure is suitable for tabular conversion.
  4. Run the conversion using the available controls.
  5. Review the generated CSV headers and rows.
  6. Copy or download the output if those options are available.
  7. Open the CSV in a spreadsheet application to verify that the columns and values are correct.

If your JSON contains nested objects or arrays, check how the converter handles them before using the resulting CSV in another application.

Method 2: Convert JSON to CSV using Python

Python is useful for data analysis, automation, reporting, and backend development.

The built-in json and csv modules can convert a JSON array of objects into a CSV file without installing additional packages.

Create a file named convert_json_to_csv.py:

import csv
import json

input_file = "input.json"
output_file = "output.csv"

with open(input_file, "r", encoding="utf-8") as json_file:
    records = json.load(json_file)

if not isinstance(records, list):
    raise ValueError("Expected a JSON array of objects.")

if not all(isinstance(record, dict) for record in records):
    raise ValueError("Every array item must be a JSON object.")

fieldnames = list(
    dict.fromkeys(
        key
        for record in records
        for key in record.keys()
    )
)

with open(
    output_file,
    "w",
    encoding="utf-8-sig",
    newline=""
) as csv_file:
    writer = csv.DictWriter(
        csv_file,
        fieldnames=fieldnames,
        extrasaction="raise"
    )

    writer.writeheader()
    writer.writerows(records)

print(f"Converted {len(records)} records to {output_file}")

Create an input.json file:

[
  {
    "id": 1,
    "name": "Amit",
    "city": "Bengaluru"
  },
  {
    "id": 2,
    "name": "Priya",
    "city": "Bhubaneswar"
  }
]

Run the script:

python convert_json_to_csv.py

The script generates an output.csv file:

id,name,city
1,Amit,Bengaluru
2,Priya,Bhubaneswar

This implementation collects column names from all records, so a field appearing only in a later object is not automatically discarded. Missing fields are written as empty cells.

The utf-8-sig encoding can help spreadsheet applications recognize UTF-8 text correctly. It adds a byte order mark to the output file, which some tools may not require.

This script expects a JSON array of objects with unique keys. It does not flatten nested objects or convert nested arrays into separate rows.

Method 3: Convert JSON to CSV using JavaScript

JavaScript is useful when you are developing a browser-based utility, a Node.js script, or an application that exports API data.

The following example converts a simple JSON array into CSV:

const records = [
  { id: 1, name: "Amit", city: "Bengaluru" },
  { id: 2, name: "Priya", city: "Bhubaneswar" }
];

const headers = [
  ...new Set(records.flatMap((record) => Object.keys(record)))
];

function escapeCsvValue(value) {
  const text = value == null ? "" : String(value);
  return `"${text.replace(/"/g, '""')}"`;
}

const csvRows = [
  headers.map(escapeCsvValue).join(","),
  ...records.map((record) =>
    headers
      .map((header) => escapeCsvValue(record[header]))
      .join(",")
  )
];

const csv = csvRows.join("\r\n");

console.log(csv);

Output:

"id","name","city"
"1","Amit","Bengaluru"
"2","Priya","Bhubaneswar"

This example quotes every field and escapes embedded double quotation marks, which helps preserve commas and line breaks in field values.

It is intended for simple arrays of objects. Nested objects and arrays are converted to strings by this implementation, so production applications should define an explicit policy for handling complex values.

Example: JSON Array to CSV

Let's walk through a complete example using product information.

Step 1: Start with JSON input

Suppose an API returns the following product data:

[
  {
    "product_id": "P101",
    "product_name": "Keyboard",
    "price": 1200,
    "category": "Accessories"
  },
  {
    "product_id": "P102",
    "product_name": "Wireless Mouse",
    "price": 500,
    "category": "Accessories"
  },
  {
    "product_id": "P103",
    "product_name": "Monitor",
    "price": 8500,
    "category": "Electronics"
  }
]

Step 2: Identify the column headers

The object keys become the CSV headers:

  • product_id
  • product_name
  • price
  • category

Each object contributes one row beneath these headers.

Step 3: Generate the CSV output

product_id,product_name,price,category
P101,Keyboard,1200,Accessories
P102,Wireless Mouse,500,Accessories
P103,Monitor,8500,Electronics

You can now import the CSV into a spreadsheet, use it for reporting, or share it with another application that accepts CSV files.

Step 4: Handle special characters

Consider a product name containing a comma:

[
  {
    "product_id": "P104",
    "product_name": "Keyboard, Mechanical",
    "price": 2500
  }
]

The corresponding CSV must quote the product name:

product_id,product_name,price
P104,"Keyboard, Mechanical",2500

Without quotation marks, the comma could be interpreted as a column separator.

Step 5: Verify the result

After conversion, check that:

  • Every product appears in the output.
  • The column headers are correct.
  • Values remain under their intended columns.
  • Commas and quotation marks inside values are escaped correctly.
  • Missing fields are handled consistently.
  • The CSV can be opened by the intended spreadsheet or data-processing application.

These checks help prevent incorrect reports and data imports.

Common Conversion Issues

Even when a JSON document is valid, converting it into a flat CSV file can introduce unexpected results. The following issues are among the most common.

1. JSON is not an array of objects

Many simple converters expect an array like this:

[
  {
    "name": "Amit",
    "age": 30
  },
  {
    "name": "Priya",
    "age": 28
  }
]

However, an API might return a wrapper object:

{
  "success": true,
  "data": [
    {
      "name": "Amit",
      "age": 30
    },
    {
      "name": "Priya",
      "age": 28
    }
  ]
}

The actual records are inside the data property rather than at the root.

How to fix it: Identify the array containing the records and extract it before conversion, if your converter requires a top-level array.

2. Nested objects create unexpected columns

Consider:

[
  {
    "id": 1,
    "name": "Amit",
    "address": {
      "city": "Bengaluru",
      "state": "Karnataka"
    }
  }
]

One option is to flatten the nested object:

id,name,address.city,address.state
1,Amit,Bengaluru,Karnataka

Another option is to keep the complete address as a JSON string inside a single CSV cell.

How to fix it: Choose a flattening convention, such as dot-separated property names, and verify that the converter supports it.

3. Arrays inside JSON objects

Consider a record with multiple skills:

[
  {
    "name": "Amit",
    "skills": ["C", "Python", "Embedded Systems"]
  }
]

Possible CSV representations include a JSON string in one cell:

name,skills
Amit,"[""C"",""Python"",""Embedded Systems""]"

Alternatively, the skills can be joined into a single text field:

name,skills
Amit,"C; Python; Embedded Systems"

The second representation is easier to read, but it may be ambiguous if the original values contain the chosen separator.

How to fix it: Decide whether to serialize arrays, join their elements, or expand them into multiple rows. Choose the method based on how the CSV will be used.

4. Missing fields in some records

Consider:

[
  {
    "id": 1,
    "name": "Amit",
    "city": "Bengaluru"
  },
  {
    "id": 2,
    "name": "Priya"
  }
]

A converter can produce:

id,name,city
1,Amit,Bengaluru
2,Priya,

The second record has no city property, so its corresponding CSV cell is empty.

An empty cell is not the same as the literal text null. Some converters may also omit columns based on their configuration.

How to fix it: Check that all expected headers are included and decide how missing properties should be represented.

5. Commas, quotation marks, and line breaks

CSV fields containing commas, double quotation marks, or line breaks must be escaped according to CSV conventions.

For example:

[
  {
    "name": "Amit",
    "comment": "He said \"Hello, team!\""
  }
]

Correct CSV:

name,comment
Amit,"He said ""Hello, team!"""

A proper CSV writer handles these escaping rules automatically.

How to fix it: Use a CSV library or a converter that supports standard CSV quoting instead of joining values with commas manually.

6. Numbers, booleans, and null values

JSON supports native data types:

[
  {
    "id": 101,
    "active": true,
    "score": 95,
    "remarks": null
  }
]

CSV does not have dedicated types for numbers, booleans, or null values. They are represented as text or empty fields.

For example, a converter might generate:

id,active,score,remarks
101,true,95,

The empty cell does not necessarily indicate whether the original JSON value was null, an empty string, or a missing property.

How to fix it: Define how each value type should be represented before conversion, especially if the CSV will later be imported into a database.

7. Duplicate or inconsistent object keys

JSON objects should not rely on duplicate property names. When duplicate keys appear in JSON input, parsers may retain only one value, making the original data ambiguous.

Similarly, records with different keys can produce inconsistent columns if the converter only examines the first object.

How to fix it: Validate the source data and make sure the converter considers all records when determining the column headers.

8. Invalid JSON syntax

A JSON parser will reject malformed input such as:

[
  {
    "name": "Amit",
    "age": 30,
  }
]

The trailing comma after 30 is not valid in standard JSON.

Correct version:

[
  {
    "name": "Amit",
    "age": 30
  }
]

How to fix it: Validate the JSON before conversion. Check for trailing commas, missing quotation marks, unbalanced brackets, and missing separators.

9. Large JSON files

Large JSON files may require substantial memory because the complete document is often loaded before conversion begins.

How to fix it: Use a streaming JSON parser or process the data in manageable chunks when working with very large datasets. Confirm that chunked processing preserves a consistent set of CSV headers.

Convert JSON with the IamNoobs Tool

The IamNoobs JSON to CSV Converter provides a convenient way to transform compatible JSON data into a tabular format without writing a conversion script.

It can be useful for API responses, exported records, product catalogs, test data, and everyday development tasks.

Example: Convert employee records

Start with this JSON:

[
  {
    "id": 101,
    "name": "Amit",
    "department": "Engineering"
  },
  {
    "id": 102,
    "name": "Priya",
    "department": "Marketing"
  }
]

The expected CSV structure is:

id,name,department
101,Amit,Engineering
102,Priya,Marketing

Steps to use the converter

  1. Open the IamNoobs JSON to CSV Converter.
  2. Paste the JSON data into the input field or upload a file if supported.
  3. Start the conversion using the available controls.
  4. Review the column headers and generated rows.
  5. Check any nested values or empty fields carefully.
  6. Copy or download the CSV using the options provided by the tool.

The exact options depend on the current implementation of the converter.

Checklist before using the CSV

  • Correct records: Confirm that all expected objects appear in the output.
  • Correct headers: Make sure each property maps to the intended column.
  • Nested data: Verify that nested objects and arrays have been handled as expected.
  • Special characters: Check values containing commas, quotes, and line breaks.
  • Missing fields: Confirm that empty cells have the intended meaning.
  • Spreadsheet compatibility: Open the output in your target application and verify that it displays correctly.

If you are converting confidential information, check the tool's privacy and data-handling practices before submitting it. For sensitive data, local conversion may be preferable.

You can also read our related tutorial, How to Convert CSV to JSON, to learn the reverse conversion process.

Frequently Asked Questions

1. How do I convert JSON to CSV for free?

You can use a free online converter or write a script using Python or JavaScript. The IamNoobs JSON to CSV Converter is an option for converting compatible JSON data through your browser.

2. What JSON format is best for CSV conversion?

An array of objects with consistent properties is usually the easiest format to convert. Each object becomes a row, and its properties become columns.

3. Can I convert nested JSON to CSV?

Yes, but nested objects and arrays need a conversion strategy. Nested objects can be flattened into columns, while arrays can be serialized, joined into text, or expanded into multiple rows.

4. Does JSON to CSV conversion preserve data types?

CSV has no built-in type system, so JSON numbers, booleans, and strings are generally represented as textual cell values. The receiving application may infer types when opening or importing the file.

5. What happens when a JSON property is missing?

A converter may create an empty cell for a missing property. The exact behavior depends on the tool and its configuration. An empty cell should not automatically be interpreted as the original JSON value null.

6. How do I convert JSON to CSV in Python?

Use Python's json module to load the JSON array and the csv module to write the records. For an array of objects, csv.DictWriter can generate the headers and corresponding rows.

7. Can I convert an API response to CSV?

Yes. If the API returns an array of objects, you can usually convert it directly. If the records are nested inside a wrapper object, first extract the array that contains the data.

8. Why does my CSV contain extra columns?

Extra columns may appear when different JSON objects contain different properties or when nested objects are flattened. Inspect the source JSON and choose a consistent column schema.

9. Can CSV store arrays or nested objects?

CSV does not natively support arrays or nested objects. These values can be serialized into text, flattened into columns, or expanded into multiple rows depending on your requirements.

10. Is JSON to CSV conversion safe for private data?

It depends on how the conversion tool processes and stores your input. For confidential records, verify the service's privacy practices or use a local conversion script that keeps the data on your own machine.

Conclusion

Converting JSON to CSV makes structured data easier to analyze, share, and import into spreadsheet applications. The process works best when your JSON contains an array of objects with consistent properties.

For reliable results, pay attention to nested structures, missing fields, special characters, and the difference between JSON data types and CSV text values. Validate the output before using it in a report, database import, or application.

For a quick conversion, try the IamNoobs JSON to CSV Converter. If you need to automate the process or handle large datasets, Python and JavaScript offer flexible options for building a repeatable workflow.