Convert CSV rows To multiple JSON using Python & pandas in Jupyter notebook

3-step Python code in Jupyter notebook

csv row to separate JSON file

Have you tried to search the internet for a simple solution to your problem but not found a suitable one and so started exploring and designing the solution yourself. Yeah! Few times! That’s what I did when I couldn’t find a perfect solution to convert CSV rows to multiple JSON files.

This article classifies as a short course on how to convert CSV to separate JSON files using Python & pandas

Convert CSV to separate JSON files using Python & pandas

Python to convert each CSV row to individual JSON file using Python code (pandas) in Jupyter notebook

Course Provider: Person

Course Provider Name: Jatin Grover

Editor's Rating:

I found many sites and blogs with code to convert CSV to JSON files using Python or Perl but they converted entire CSV to a combined JSON file.

All of them were simple and worked well except that all had the basic requirement missing. My requirement was to convert CSV to multiple JSON files to be added to Mongo DB as part of the larger project I am working on. Since I couldn’t find any solution, I decided to make my own 2-step code in python.

Steps to convert CSV to multiple JSON files using Python:

Following are the detailed steps and code written in Jupyter notebook to take CSV as an input and output a separate JSON for each CSV row i.e. effectively convert each CSV row to different JSON file.

  1. Open the CSV file to convert it’s row to individual JSON files and read the contents in a variable using DictReader() function — this function returns an OrderedDict.
csv file rows to separate JSON

2. Now, to convert this file’s CSV rows to multiple JSON files, we use json.dumps(). So, every row in CSV presented by this OrderedDict, a separate JSON file using json.dumps() will be created. In a way, this implementation of json.dumps() outputs separate JSON for each row in CSV file you provide as input

csv rows to multiple json

FiveStepGuide may earn an Affiliate Commission if you purchase something through links on this page.

Rank High in Google / Bing by writing content with optimal Readability. SEO tools to improve Readability. Click the video image below to start your 30-day FREE trial

linkedin readability

3. Close the streams.

csv file rows fully converted to many JSON files

This will create individual JSON files equal to the number of rows in CSV. So our convert CSV to multiple JSON files mission is accomplished here 🙂

Hope this helped.

The same post was first written on Medium. Please click here to check out my initial post on medium.

Thanks for reading this post. I have several other such quick fixes and simple solutions to real-world tough looking problems in Technology sections on this website.

If you liked this post, kindly comment and like using the comment form below.

What is a CSV file

A CSV is a comma-separated values file having data in a tabular format. Every row has data delimited by a comma. For example, if you copy the following data to notepad and save as tryme.csv, you will be able to open it in Microsoft Excel and see or yourself.
Name, City, Age
Justin, Singapore, 40
Anthony, New York, 38
Mary, London, 29

What is a JSON file?

JSON is an abbreviation for JavaScript Object Notation. It is a novel file format to store information in an organized, computer-readable and workable mannerFor instance, the first row of the CSV defined above will be written in JSON as follows:
“Name” : “Justin”,
“City” : “Singapore, MT”,
“Age” : “40”

Suggested articles based on your reading history:

3 thoughts on “Convert CSV rows To multiple JSON using Python & pandas in Jupyter notebook<p class='sub-heading'>3-step Python code in Jupyter notebook </p>”

Leave a Comment

Five Ste? Guide
Follow me