How To Parse and Convert JSON to CSV using Python

by Shahid Ayoub

JSON is an acronym standing for JavaScript Object NotationThe json library in python can parse JSON from strings or files. The library parses JSON into a Python dictionary or list. We come across various circumstances where we receive data in json format and we need to send or store it in csv format. Since the csv files can easily be opened using LibreOffice Calc in ubuntu or Microsoft Excel in windows the need for json to csv conversion usually increases.

Parsing JSON

Let us take an example of  JSON in string form as shown below :


json_data = ‘{“name”: “smith”, “email”: “”}’

The json_data will be parsed like this in python:

import json

json_parsed = json.loads(json_data)

Now json.loads() function parses the json string data and it can be used as a normal dictionary in python. And we can access the values using keys. For example, here we can access email Id using key ‘email’ as shown below:

print json_parsed[‘email’]

This is a simple example of parsing JSON, but we can have JSON file where data is much more complex . For example JSON data where values are lists and are accessed using list index as shown below :

Data = '{"employee_name": "James", "email": "", "job_profile": ["Team Lead", "Sr. Developer"]}'

It can be parsed like this :

Parsed_data = json.loads(Data)

print Parsed_data[‘job_profile’][0]

"Team Lead"

The complexity increases further for JSON data where values are lists having dictionaries within. Again values will be accessed using list index and key value pair as shown below:


Data = '{"employee_name": "James", "email": "", "job_profile": [{"title1":"Team Lead", "title2":"Sr. Developer"}]}'

It can be parsed like this:

Parsed_data = json.loads(Data)

print Parsed_data[‘job_profile’][0][‘title1’]

"Team Lead"

Similarly other fields can be accessed from the parsed data.

Converting JSON to CSV using Python:

CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. CSV file format separates values using commas as delimiters . For simple JSON data, keys will be headers for the CSV file and values the descriptive data. For example we have thousands of records about employees working in an organisation as shown below:

employee_data = '{"employee_details":[{"employee_name": "James", "email": "", "job_profile": "Sr. Developer"},{"employee_name": "Smith", "email": "", "job_profile": "Project Lead"},.....]}'

This can be parsed and converted to CSV using python as shown below:

import json

import csv

employee_parsed = json.loads(employee_data)

emp_data = employee_parsed['employee_details']

# open a file for writing

employ_data = open('/tmp/EmployData.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(employ_data)

count = 0

for emp in emp_data:

      if count == 0:

             header = emp.keys()


             count += 1



The CSV file will look like this for few records  :

employee_name email job_profile
James Sr. Developer
Smith Project Lead


The python program written above will open a csv file in tmp folder and write the content of JSON file into it and close it at the end. Make sure to close the file at the end in order to save the contents.

JSON files can have much more complex structures than CSV files, so a direct conversion is not always possible. The above mentioned examples will lay the foundation for converting JSON data with high complexity to CSV. 

19 thoughts on “How To Parse and Convert JSON to CSV using Python”

    1. It seems you are using the employee JSON data as it is. You have to strip dots(…) at the end it is an example or you can add more instances there.

  1. There seems an encoding issue, use proper quotes for the the key and string values. I was able to parse the data after replacing the existing quotes with normal double quotes or use proper encoding.

  2. Hi,

    I have a variable called user_info where profile data of users in stored. The output is supposed to in json.
    When I run the program (it’s a bot), I’d like to generate a json file to store all the data as json, and later export the data as CSV. I’ve been looking for a solution for hours now and I’ve found nothing. I’m new to Python. Do you know how to do that?


  3. Which one is easier to parse and convert into CSV: JSON or XML?
    I can get my data in either of them and would like to make a decision which one to proceed with.

  4. Hello Shahid pretty well your post, actually that helps me a lot.
    You can check my JSON example here following your approach I have already created the CSV file. however and that could be a very stupid question I have tried to do two things:
    1. just get a couple of values from ‘stations’ (id,n,da,dx,ba,bx)
    1.1 rename the output with a different name header
    2. I don’t know why my CSV file has an enter after each row 🙁

    Probably you are busy with another stuff but my friend I would really appreciate if you can guide me

    thanks so much

    1. Buddy i played a little bit and i found that:

      import json
      import csv

      #read the json file
      with open(‘stations.json’,’r’) as bixi_stations:
      data = json.load(bixi_stations)

      data_csv = data[‘stations’]

      file_data= open( ‘C:/Users/axu30/PycharmProjects/First/stations.csv’,’w’)
      csvwriter = csv.writer(file_data)

      liste = {}
      i =0
      for count in data_csv:
      if i == 0:
      header = ‘Id’,’Station Name’,’Station Id’,’Empty places’,’Qty Avail Bikes’
      i += 1

      liste[‘s’] = count[‘s’]
      liste[‘n’] = count[‘n’]
      liste[‘da’] = count[‘da’]
      liste[‘ba’] = count[‘ba’]



  5. hi I want to convert one json file to csv , below is file

    i am trying all possible way but could not get it. please help me.

  6. Hi,
    First, thank you for this. Its really helpful. I’m having an issue where one row in my data will occasionally be missing a column and columns past that one are now shifted one to the left. Any thoughts on how to fix that?

Leave a Reply

Your email address will not be published. Required fields are marked *

Tools & Practices

Tools and Technologies we use at Applied

Contact us now

Popular Posts