XhCode Online Converter Tools

JSON To CSV Converter

Enter json here:
Results:
JSON To CSV

Converting JSON to CSV involves transforming the data structure from JSON format (which is hierarchical and nested) to a tabular format like CSV. This is useful for working with data in spreadsheets or databases.

Example JSON:
json

[
{
"Name": "John",
"Age": 28,
"Country": "USA"
},
{
"Name": "Jane",
"Age": 22,
"Country": "Canada"
},
{
"Name": "Tom",
"Age": 30,
"Country": "UK"
}
]
Converted CSV Output:
pgsql

Name,Age,Country
John,28,USA
Jane,22,Canada
Tom,30,UK
How to Convert JSON to CSV
1. Using JavaScript (For Web Use)
You can use JavaScript in a web browser to convert a JSON object into CSV format. Here is an example that does this conversion:

JavaScript Code:
html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON to CSV</title>
</head>
<body>
<h2>Convert JSON to CSV</h2>
<button onclick="generateCSV()">Generate CSV</button>
<pre id="csv-output"></pre>

<script>
const jsonData = [
{
"Name": "John",
"Age": 28,
"Country": "USA"
},
{
"Name": "Jane",
"Age": 22,
"Country": "Canada"
},
{
"Name": "Tom",
"Age": 30,
"Country": "UK"
}
];

function generateCSV() {
// Extract the keys (header row)
const headers = Object.keys(jsonData[0]);

// Create the CSV format
let csv = headers.join(",") + "\n"; // Add headers to the CSV

// Loop through the data and add each row
jsonData.forEach(item => {
const row = headers.map(header => item[header]).join(",");
csv += row + "\n";
});

// Display the CSV in the <pre> tag
document.getElementById("csv-output").textContent = csv;
}
</script>
</body>
</html>
Explanation:
The jsonData variable holds your JSON data.
The generateCSV() function:
Extracts the keys of the JSON object (the column names).
Loops through the JSON array and creates a CSV string.
The CSV output is displayed in the <pre> tag, preserving line breaks and formatting.
Steps:
Copy and paste the code into an HTML file.
Open the file in a web browser.
Click the "Generate CSV" button to generate the CSV output.
2. Using Python (For Automated or Bulk Conversion)
For more control or to automate the conversion, you can use Python to convert JSON to CSV, especially useful if you need to process large files or multiple datasets.

Python Script:
python

import json
import csv

# Sample JSON data
json_data = [
{"Name": "John", "Age": 28, "Country": "USA"},
{"Name": "Jane", "Age": 22, "Country": "Canada"},
{"Name": "Tom", "Age": 30, "Country": "UK"}
]

# Specify the CSV output file
output_file = 'output.csv'

# Write to CSV
with open(output_file, 'w', newline='') as csvfile:
# Create a CSV writer
writer = csv.DictWriter(csvfile, fieldnames=json_data[0].keys())

# Write the header (keys)
writer.writeheader()

# Write the rows (data)
writer.writerows(json_data)

print(f"CSV file '{output_file}' has been created.")
Explanation:
The json_data variable contains the JSON object you want to convert.
csv.DictWriter() is used to write the JSON data into CSV format, where each dictionary is treated as a row in the CSV file.
The CSV file is saved as output.csv.
Steps:
Install Python (if not already installed).
Run the Python script with your JSON data and specify the output CSV file name.
The script will generate a CSV file.
3. Using Online Tools
If you don't want to write code, you can use online tools to convert JSON to CSV:

JSON to CSV Converter:

Paste your JSON data into the provided text box or upload a JSON file.
The tool will automatically convert it to CSV, which you can then download.
ConvertCSV:

Upload your JSON file or paste the JSON data.
The tool will generate a downloadable CSV file for you.
4. Manually Writing CSV from JSON
For small JSON datasets, you can manually convert the data to CSV by:

Extracting the keys (column names) from the JSON objects.
Writing each object's values as a new row.
For the following JSON:

json

[
{ "Name": "John", "Age": 28, "Country": "USA" },
{ "Name": "Jane", "Age": 22, "Country": "Canada" },
{ "Name": "Tom", "Age": 30, "Country": "UK" }
]
The manually written CSV would be:

pgsql

Name,Age,Country
John,28,USA
Jane,22,Canada
Tom,30,UK
Summary of Methods:
JavaScript: Use JavaScript in a web browser for quick conversions and dynamic CSV generation.
Python: Use Python with the csv module for automated and bulk conversions, especially for large files.
Online Tools: Websites like JSON to CSV Converter and ConvertCSV allow you to convert JSON to CSV quickly without coding.
Manual Writing: For small datasets, you can manually convert JSON to CSV by extracting keys and values.