![]() |
VOOZH | about |
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
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.
Output
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.
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.
Output
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.
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.
Output
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.