CSV to JSON Converter
JSON Output:
Converting CSV (Comma-Separated Values) to JSON (JavaScript Object Notation) involves transforming tabular data into a hierarchical format. Here’s a comprehensive guide on how to achieve this:
Steps to Convert CSV to JSON
1. Understand CSV and JSON Formats
- CSV (Comma-Separated Values): A simple text format for tabular data where each line represents a row, and columns are separated by commas.
- JSON (JavaScript Object Notation): A hierarchical data format that stores data in key-value pairs and can include nested structures.
Example CSV:
name,age,city
John,30,New York
Jane,25,Los Angeles
Example JSON:
[
{
"name": "John",
"age": 30,
"city": "New York"
},
{
"name": "Jane",
"age": 25,
"city": "Los Angeles"
}
]
2. Convert Using Online Tools
There are several online tools available that allow you to upload a CSV file or paste CSV data to convert it to JSON.
- Examples:
3. Using Spreadsheet Software
Google Sheets:
- Import CSV:
- Open Google Sheets.
- Go to
File
>Import
>Upload
to upload your CSV file.
- Export as JSON:
- Use a script or add-on to convert the data to JSON format.
- Example add-on: Google Sheets JSON Exporter
Microsoft Excel:
- Import CSV:
- Open Excel and import the CSV file.
- Go to
Data
>Get Data
>From File
>From Text/CSV
.
- Convert to JSON:
- Export the data and use an online converter or script to convert the exported data to JSON.
4. Using Programming Languages
You can use programming languages to automate the conversion process.
Python:
import pandas as pd
# Read CSV file
df = pd.read_csv(‘data.csv’)
# Convert to JSON
json_data = df.to_json(orient=‘records’)
# Save JSON to file
with open(‘data.json’, ‘w’) as f:
f.write(json_data)
JavaScript (Node.js):
const fs = require('fs');
const csv = require('csvtojson');
// Read CSV file and convert to JSONcsv()
.fromFile(‘data.csv’)
.then((jsonObj) => {
// Save JSON to file
fs.writeFileSync(‘data.json’, JSON.stringify(jsonObj, null, 2));
});
5. Using Command-Line Tools
csvkit (Python-based command-line tool):
- Install csvkit:
pip install csvkit
- Convert CSV to JSON:
csvjson data.csv > data.json
Summary
Converting CSV to JSON involves transforming the flat, tabular CSV data into a hierarchical JSON format. You can use online tools, spreadsheet software, or programming languages like Python and JavaScript to achieve this. Each method provides different features and flexibility depending on your needs.
Popular Converters
Fuel Economy |
|||