Python convert json to csv – How To Convert JSON To CSV File in Python?

What do you mean by CSV File?

Python convert json to csv: A CSV file, which stands for Comma Separated Values file, is a simple text file that maintains a list of data. CSV files are commonly used to exchange data between different applications. Contact Managers and Databases, for example, typically support CSV files.

These CSV files are also known as Comma Separated Values or Comma Delimited Files. These files primarily use the comma character to delimit or segregate data. However, other characters, such as semicolons, are sometimes used. The plan is to export complex data from one program to a CSV file and then import the data from the CSV file into another program.

A Comma Separated Values (CSV) file has a simple structure that contains some data that is listed and separated by commas. CSV files are constructed in such a way that they may simply import and export data from other applications. The resulting data is easily readable by humans and may be seen using a text editor like Notepad or a spreadsheet program like Microsoft Excel or Google Sheets.

What do you mean by JSON Array?

Convert json to csv python: JSON (JavaScript Object Notation) is a dictionary-like notation that may be utilized in Python by importing the JSON module. Every record (or row) is preserved as its own dictionary, with the column names serving as the dictionary’s Keys. To make up the whole dataset, all of these records are kept as dictionaries in a nested dictionary. It is saved together with the extension. geeksforgeeks.json

JSON format was actually based on a subset of JavaScript. It is, nevertheless, referred to as a language-independent format, and it is supported by a wide range of programming APIs. In most cases, JSON is used in Ajax Web Application Programming. Over the last few years, the popularity of JSON as an alternative to XML has gradually increased.

While many programs use JSON for data transfer, they may not keep JSON format files on their hard drive. Data is exchanged between computers that are linked via the Internet.

Convert JSON To CSV File in Python

Python write json to csv: Let us consider the below JSON file as an example:

samplefile.json:

{
   "Name":"Vikram",
   "Branch":"Cse",
   "year":2019,
   "gpa":[
      9.1,
      9.5,
      9.6,
      9.2
   ]
}

Now we create an empty CSV file say demo.csv.

Here, we convert the above-given JSON array file data into a CSV file and store it in demo.csv.

Approach:

  • Import csv module using the import keyword.
  • Import json module using the import keyword.
  • Open some random JSON file in read-only mode using the open() function and store it in a variable
  • Open an empty CSV file in write mode using the open() function and store it in another variable
  • Pass the above-given json file to the load() function of the json module to convert the json file data into a dictionary
  • Pass the above-given csv file to writer() function of the csv module to pass the given csv to the writer() function of csv file and store it in a variable to write content/data to csv file
  • Write all the key values of the json file using writerow() function and apply it to the above writing object
  • Write all the dictionary values of the json file using writerow() function and apply it to the above writing object
  • Close the given JSON file using the close() function.
  • Close the given CSV file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Import csv module using the import keyword.
import csv  
# Import json module using the import keyword.
import json  
# Open some random JSON file in read-only mode using the open() function 
# and store it in a variable
gvn_jsonfile =open('samplefile.json','r')
# Open an empty CSV file in write mode using the open() function 
# and store it in another variable
gvn_csvfile=open('demo.csv','w')

# Pass the above given json file to the load() function of the json module
# to convert the json file data into a dictionary
dictionary =json.load(gvn_jsonfile)
# Pass the above given csv file to writer() function of the csv module to 
# pass the given csv to writer() function of csv file and store it in a variable to write content/data to csv file
write=csv.writer(gvn_csvfile)
# Write all the key values of the json file using writerow() function and 
# apply it on the above writing object 
write.writerow(dictionary.keys())
# Write all the dictionary values of the json file using writerow() function and 
# apply it on the above writing object 
write.writerow(dictionary.values())

# Close the given JSON file using the close() function
gvn_jsonfile.close()
# Close the given CSV file using the close() function
gvn_csvfile.close()

Output:

csv output of the json file