The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted string which contains the value in key-value mapping within { }. It is similar to the dictionary in Python.
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format.
Refer to the below articles to understand the basics of JSON and CSV.
Converting JSON to CSV
For a simple JSON data consisting of key and value pairs, keys will be headers for CSV file and values the descriptive data.
Example: Suppose the JSON file looks like this:

We want to convert the above JSON to CSV file with key as headers.
# Python program to convert # JSON file to CSV import json import csv # Opening JSON file and loading the data # into the variable data with open('data.json') as json_file: data = json.load(json_file) employee_data = data['emp_details'] # now we will open a file for writing data_file = open('data_file.csv', 'w') # create the csv writer object csv_writer = csv.writer(data_file) # Counter variable used for writing # headers to the CSV file count = 0 for emp in employee_data: if count == 0: # Writing headers of CSV file header = emp.keys() csv_writer.writerow(header) count += 1 # Writing data of CSV file csv_writer.writerow(emp.values()) data_file.close() |
Output:

Recommended Posts:
- Convert CSV to JSON using Python
- Convert multiple JSON files to CSV Python
- Python program to read CSV without CSV module
- Saving Text, JSON, and CSV to a File in Python
- Python - Difference between json.dump() and json.dumps()
- Convert CSV to Excel using Pandas in Python
- Convert CSV to HTML Table in Python
- Convert HTML table into CSV file in python
- Create a GUI to convert CSV file into excel file using Python
- Convert Excel to CSV in Python
- How to Convert an image to NumPy array and saveit to CSV file using Python?
- Convert Text File to CSV using Python Pandas
- How to convert an array to CSV file in PHP ?
- Convert a NumPy array into a csv file
- Python | Ways to convert string to json object
- Convert JSON to dictionary in Python
- Convert Text file to JSON in Python
- Python - Convert JSON to string
- How To Convert Python Dictionary To JSON?
- How to convert pandas DataFrame into JSON in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

