. Advertisement .
..3..
. Advertisement .
..4..
How to convert JSON to CSV in Python Pandas is a problem many programmers still try to figure out. Our guidelines will help you speed up the process with three simple steps. Keep scrolling for more guidance.
What Are JSON and CSV?
- JSON
JSON is a data format based on text, which follows the syntax of Javascript objects and has become popular thanks to Douglas Crockford.
JSON and the literal syntax of JavaScript objects share a lot of things in common. Yet, do not assume that JSON is dependent on Javascript; in fact, both of them can operate separately. Also, numerous program environments incorporate JSON generating and reading abilities.
In essence, JSON serves as a typical string, which is quite practical if you wish to perform efficient data transmission via a network. And if you need to gain access to the data, it is important to convert the data into native Javascript objects.
Such issues are nothing to worry about, as JavaScript offers global JSON objects whose methods for conversion are available.
- CSV
CSV stands for Comma Separated Values. A CSV file is simply a type of text file containing a data list. CSV files are usually adopted to help programmers exchange data across different applications.
For instance, contact managers and databases tend to use CSV files. Other common names for CSV files are Comma Delimited or Character Separated Value files.
As their names suggest, CSV files employ commas to delimit or separate data. Sometimes, however, other types of characters, such as semicolons, can still be adopted.
The notion here is that programmers can export complicated data from other applications to CSV files, before importing data from those CSV files into yet another application.
When it comes to structures, CSV’s structures are fairly simple. It is just a data list whose values are kept apart by commas. Let’s say a contact manager has several contacts, which you wish to export in CSV files. The results you get will contain text as follows:
Example 0 (CSV file)
Name,Phone Number,Email,Address
Bob Adam,098-765-743, [email protected],Hills Street
Eva Lee, 234-876-981,[email protected],Mountain Avenue
How To Convert JSON to CSV in Python Pandas
Step 1. Get The JSON Data
Suppose you own a file named export.json, whose contents are as follows.
Example 1 (JSON data)
{
"0": {
"Netflix": "Stranger Things",
"Quibi": "Most Dangerous Game"
},
"1": {
"Netflix": "Money Heist",
"Quibi": "The Stranger"
},
"2": {
"Netflix": "House of Cards",
"Quibi": "50 States of Fright"
},
"3": {
"Netflix": "Rick and Morty",
"Quibi": "Flipped"
},
"4": {
"Netflix": "Better Call Saul",
"Quibi": "Survival"
}
}
Now, we will transform it into a CSV file. The above example has all the keys indexed, which means once we transform the file into Pandas objects, its index will return 4, 3, 2, 1, and 0. The header columns become Quibi and Netflix.
Step 2. Read Your JSON and Convert It Into A Pandas Object
Example 1 (Solution):
# app.py
import pandas as pd
pdObj = pd.read_json('export.json', orient='index')
print(pdObj)
Example 1 (Output – Pandas Object):
python3 app.py
Netflix Quibi
0 Stranger Things Most Dangerous Game
1 Money Heist The Stranger
2 House of Cards 50 States of Fright
3 Rick and Morty Flipped
4 Better Call Saul Survival
Step 3. Change The Pandas Object Into A CSV File
Example 1 (Solution) (cont):
# app.py
import pandas as pd
pdObj = pd.read_json('export.json', orient='index')
csvData = pdObj.to_csv(index=False)
print(csvData)
Example 1 (Output – CSV):
python3 app.py
Netflix,Quibi
Stranger Things,Most Dangerous Game
Money Heist,The Stranger
House of Cards,50 States of Fright
Rick and Morty,Flipped
Better Call Saul,Survival
Conclusion
This article has shown how to convert JSON to CSV in Python Pandas via three simple steps. We hope that you will find our examples and instructions simple to follow. For other Pandas-related conversions (such as converting a DataFrame into lists), feel free to browse our tutorials.
Leave a comment