1. Using JavaScript (Node.js) to Convert JSON to CSV or Excel
In Node.js, you can use packages like json2csv (for CSV) or xlsx (for Excel) to convert JSON to your desired format.
Convert JSON to CSV Using Node.js
Install the json2csv package:
bash
npm install json2csv
Example Code to Convert JSON to CSV:
javascript
const { parse } = require('json2csv');
const fs = require('fs');
const jsonData = [
{ "name": "Jane Doe", "age": 28, "isEmployed": true, "city": "New York" },
{ "name": "John Smith", "age": 34, "isEmployed": false, "city": "Los Angeles" }
];
// Convert JSON to CSV
const csv = parse(jsonData);
// Save CSV to a file
fs.writeFileSync('output.csv', csv);
console.log('CSV file created!');
This will create a CSV file named output.csv with the data from jsonData.
Convert JSON to Excel Using Node.js
Install the xlsx package:
bash
npm install xlsx
Example Code to Convert JSON to Excel:
javascript
const XLSX = require('xlsx');
const fs = require('fs');
const jsonData = [
{ "name": "Jane Doe", "age": 28, "isEmployed": true, "city": "New York" },
{ "name": "John Smith", "age": 34, "isEmployed": false, "city": "Los Angeles" }
];
// Create a new workbook
const wb = XLSX.utils.book_new();
// Convert JSON to a worksheet
const ws = XLSX.utils.json_to_sheet(jsonData);
// Append the worksheet to the workbook
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
// Write the workbook to an Excel file
XLSX.writeFile(wb, 'output.xlsx');
console.log('Excel file created!');
This will create an Excel file named output.xlsx with the data from jsonData.
2. Using Python to Convert JSON to CSV or Excel
In Python, you can use the pandas library, which simplifies both CSV and Excel file handling.
Convert JSON to CSV Using Python
Install pandas:
bash
pip install pandas
Example Code to Convert JSON to CSV:
python
import pandas as pd
import json
# JSON data
json_data = [
{"name": "Jane Doe", "age": 28, "isEmployed": True, "city": "New York"},
{"name": "John Smith", "age": 34, "isEmployed": False, "city": "Los Angeles"}
]
# Convert JSON to DataFrame
df = pd.DataFrame(json_data)
# Convert DataFrame to CSV
df.to_csv('output.csv', index=False)
print('CSV file created!')
This will create a CSV file named output.csv from the provided JSON data.
Convert JSON to Excel Using Python
Install openpyxl for Excel support:
bash
pip install openpyxl
Example Code to Convert JSON to Excel:
python
import pandas as pd
# JSON data
json_data = [
{"name": "Jane Doe", "age": 28, "isEmployed": True, "city": "New York"},
{"name": "John Smith", "age": 34, "isEmployed": False, "city": "Los Angeles"}
]
# Convert JSON to DataFrame
df = pd.DataFrame(json_data)
# Convert DataFrame to Excel
df.to_excel('output.xlsx', index=False)
print('Excel file created!')
This will create an Excel file named output.xlsx from the provided JSON data.
3. Using Online Tools (No Code)
If you don't want to use code, there are several online tools that can convert JSON to CSV or Excel for you:
Convert JSON to CSV Online:
JSON to CSV Converter — Upload a JSON file or paste JSON, and it will generate a CSV file.
Convert JSON to Excel Online:
ConvertJSON.com — Upload a JSON file or paste JSON, and it will generate an Excel file.
These tools are useful for quick conversions without needing to write any code.
4. Convert JSON to CSV or Excel with Excel Itself (Manual Method)
If you want to do it manually in Excel:
Open Excel.
Go to the Data tab.
Select Get Data > From JSON.
Browse for your JSON file and load it into Excel.
Excel will automatically parse the JSON file into a table.
Once the data is displayed in Excel, you can save it as a CSV or Excel file.
5. Using JavaScript in Browser (For Web Applications)
If you want to convert JSON to CSV or Excel directly in the browser, you can use JavaScript to generate downloadable CSV or Excel files.
Convert JSON to CSV in Browser:
Here's how you can use JavaScript to generate a CSV file from JSON:
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>
<button id="download">Download CSV</button>
<script>
const jsonData = [
{ "name": "Jane Doe", "age": 28, "isEmployed": true, "city": "New York" },
{ "name": "John Smith", "age": 34, "isEmployed": false, "city": "Los Angeles" }
];
function jsonToCSV(json) {
const keys = Object.keys(json[0]);
const csv = [
keys.join(','),
...json.map(row => keys.map(key => row[key]).join(','))
].join('\n');
return csv;
}
document.getElementById('download').addEventListener('click', function() {
const csvData = jsonToCSV(jsonData);
const blob = new Blob([csvData], { type: 'text/csv;charset=utf-8;' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'output.csv';
link.click();
});
</script>
</body>
</html>
This code will allow you to download the CSV file directly from the browser when you click the "Download CSV" button.
Convert JSON to Excel in Browser:
You can also use the xlsx library for Excel file generation in the browser.
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 Excel</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.2/xlsx.full.min.js"></script>
</head>
<body>
<button id="download">Download Excel</button>
<script>
const jsonData = [
{ "name": "Jane Doe", "age": 28, "isEmployed": true, "city": "New York" },
{ "name": "John Smith", "age": 34, "isEmployed": false, "city": "Los Angeles" }
];
document.getElementById('download').addEventListener('click', function() {
const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(jsonData);
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
XLSX.writeFile(wb, 'output.xlsx');
});
</script>
</body>
</html>
This will allow you to download the Excel file from the browser with the click of a button.
Conclusion
Node.js and Python are great options for converting JSON to CSV or Excel programmatically.
Online tools are the fastest if you want to do a quick conversion without coding.
You can even do this directly in the browser with JavaScript and libraries like json2csv or xlsx.