Open In App

Convert CSV to JSON using Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting CSV to JSON using Python involves reading the CSV file, converting each row into a dictionary and then saving the data as a JSON file. For example, a CSV file containing data like names, ages and cities can be easily transformed into a structured JSON array, where each record is represented as a JSON object. Let’s explore different methods to convert CSV data into JSON format. The following is an example of our input.csv file:

Name,Age,City

John,28,New York
Alice,24,Los Angeles
Bob,30,Chicago

Using csv and json modules

This is the most basic and standard approach using built-in Python libraries. It reads CSV data into dictionaries and writes it out as a JSON file. Perfect for simple use cases and small datasets.

[GFGTABS]
Python

import csv
import json

with open('input.csv', mode='r', newline='', encoding='utf-8') as csvfile:
    data = list(csv.DictReader(csvfile))

with open('output.json', mode='w', encoding='utf-8') as jsonfile:
    json.dump(data, jsonfile, indent=4)


[/GFGTABS]

Output

Output

Using csv and json module

Explanation: csv.DictReader convert each row in the CSV into a dictionary, creating a list of dictionaries representing the entire dataset. This list is then serialized into JSON format using json.dump, with indentation for better readability and saved to an output file.

Using pandas library

Pandas is a powerful data manipulation library. It’s highly efficient for handling large datasets, performing complex operations, and converting to/from many formats including JSON.

[GFGTABS]
Python

import pandas as pd

# Read CSV file
df = pd.read_csv('input.csv')

# DataFrame to JSON
df.to_json('output.json', orient='records', lines=True)


[/GFGTABS]

Output

Output

Using pandas

Explanation: This code reads a CSV file into a pandas DataFrame using pd.read_csv. It then converts the DataFrame into a list of JSON objects (one per row) using to_json with orient=’records’ and lines=True, saving each JSON object on a new line in the output file.

Using jsonlines

The jsonlines library is used to write data in JSON Lines format—where each line is a separate JSON object. It’s useful for large datasets where line-by-line processing is needed.

[GFGTABS]
Python

import csv
import jsonlines

with open('input.csv', mode='r', newline='', encoding='utf-8') as csvfile, jsonlines.open('output.jsonl', mode='w') as writer:
    for row in csv.DictReader(csvfile):
        writer.write(row)


[/GFGTABS]

Output

Output

Using jsonlines

Explanation: The code uses csv.DictReader to convert each CSV row into a dictionary, then writes each dictionary as a separate line in a .jsonl file using jsonlines.write.


Next Article

Similar Reads