. Advertisement .
..3..
. Advertisement .
..4..
Don’t know how to convert CSV to JSON using Python? Our tutorial will show you how to do it step by step. We also give instances so that you can understand better.
Comma Separated Value (or CSV for short) files display data in a table-like format with many columns and rows. For instance, an Excel spreadsheet is one type of CSV file.
These files are identified by the .csv extension, such as itutoria.csv. Each column in this file will denote a distinct feature variable, and each row will stand in for a dataset record.
On the other hand, JavaScript Object Notation (JSON for short) is a diction-like notation that can be utilized by importing the JSON module in Python. Each row is kept as a unique dictionary, with the name of the columns serving as the dictionary’s Keys.
Each record is stored as nested dictionaries to create the whole dataset. It is identified by the .json extension. For instance, itutoria.json.
To convert CSV to JSON using Python, you can use the below template:
import pandas as pd
df = pd.read_csv (r’Path where the CSV file is saved\File Name.csv’)
df.to_json (r’Path where the new JSON file will be stored\New File Name.json’)
How To Convert CSV To JSON String Using Python
The steps for using the template above will be shown here.
Step 1: Get The CSV File Ready
Get the CSV file you want to convert ready to a JSON string.
Let’s create a CSV file named Products with the following details as an illustration:
Product Price
Desktop 700
Tablet 250
Printer 120
Laptop 1200
Step 2: Install The Pandas Package
The Pandas package should be installed if it has not previously been. Installing the Pandas under Windows can be done using the syntax shown below:
pip install pandas
Step 3: Convert The CSV To JSON String Using Python
You can now convert the CSV file into a JSON string with the help of the template below:
import pandas as pd
df = pd.read_csv (r’Path where the CSV file is saved\File Name.csv’)
df.to_json (r’Path where the new JSON file will be stored\New File Name.json’)
Here are some examples:
The following is the location of the CSV file:
C:\Users\Ron\Desktop\Test\Products.csv
In this sample, the Products is the name of the file, and the .csv denotes the file extension.
The JSON file is stored in the following path:
C:\Users\Ron\Desktop\Test\New_Products.json
In this case, New Products is the name of the newly created file, and .json denotes the file extension.
Notice that you must adjust the path to the location where your device will keep the file.
Below is the whole Python code for our example’s conversion of the CSV to JSON string:
import pandas as pd
df = pd.read_csv (r’C:\Users\Ron\Desktop\Test\Products.csv’)
df.to_json (r’C:\Users\Ron\Desktop\Test\New_Products.csv’)
Activate the Python code that is adjusted to the paths. Then, your new JSON file is generated at the location you have provided.
You can see the following string if you launch the JSON file:
{“Product”:{“0”:”Desktop Computer”,”1”:”Tablet”,”2”:”Printer”,”3”:”Laptop”},”Price”:{“0”:700,”1”:250,”2”:120,”3”:1200}}
Conclusion
So there you have a complete tutorial on “how to convert CSV to JSON string using Python“. Hopefully, you find this guide easy to understand and follow. Keep practicing, and you will soon master this technique.Want to gain more skills related to CSV files? Let’s learn how to run a CSV to dictionary conversion in Python.
Leave a comment