How to Convert CSV to JSON String Using Python?

The particular text file named  “CSV or Comma Separated Values” is used to store the tabular structured format data. On the other hand, the human-readable text file named “JSON” or “JavaScript Object Notation” file is used to store and exchange the data.

To make it easier to work with data, the “CSV” file is converted into “JSON” using different methods in Python. 

This post will list the most used methods to convert CSV to JSON string using Python:

  • Method 1: Using df.to_json() Function
  • Method 2: Using CSV and JSON Module

Method 1: Using df.to_json() Function

The “df.to_json()” function is used to convert the given DataFrame to JSON string. The syntax of the “df.to_json()” function is shown below:

df.to_json()

The “orient=” parameter is used to format the returned string. The “indent=” parameter specifies the length of the whitespace used in the returned JSON string.

The CSV file used in all upcoming examples is shown below:

Code:

import pandas as pd
data_frame = pd.read_csv (r'C:\Users\p\Documents\program\example.csv')
data_frame.to_json (r'C:\Users\p\Documents\program\example.json')

The “pd.read_csv()” function is used to read the CSV file data from the specified path. The “df.to_json()” converts the CSV data into JSON string and creates the JSON file in the specified path.

Output: 

The CSV file has been converted to JSON string.

Method 2: Using CSV and JSON Module

The CSV and JSON module is used to convert the CSV or comma-separated value to JSON string. Let’s take the following CSV data as an example:

Code: 

import csv
import json

data = {}
with open('example.csv', 'r') as f:
    reader = csv.DictReader(f)
    for rows in reader:
        key = rows['Name']
        data[key] = rows

with open('example.json', 'w') as jf:
    json_str = json.dumps(data, indent=4)
    jf.write(json_str)
  • The “csv” and “json” modules are imported at the start using the “import” keyword.
  • The empty dictionary is assigned to a variable named “data”.
  • The “with open()” function is used to open the CSV file in “r” reading mode.
  • The “csv.DictReader()” is used to read each row of the CSV file as a dictionary with column names as keys.
  • The “for loop” iterates over the rows of the reader object and assigns each row to the variable named “rows”.
  • The “with open()” function is utilized to open the file in “w” writing mode.
  • The “json.dumps()” function accepts the reader data and indent value as an argument and writes it into a JSON file as JSON string.

Output: 

The CSV  file has been converted into a JSON string.

Conclusion

The “df.to_json()” function of the Pandas module and the CSV module, along with the JSON module, is used to convert the CSV to JSON string in Python. The “df.to_json()” function is used along with the “pd.read_csv()” function to read and convert CSV to JSON string. Similarly, the “csv.DictReader()” function of the CSV module is used along with the “json.dumps()” function to convert the CSV to JSON string. This blog presented multiple methods to convert CSV to JSON string using numerous examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×