Click here to Skip to main content
15,851,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Tried using json_normalize() , flatten module as well. But looking for a generic function which would be able to convert any nested JSON file to CSV.

eg. JSON file ::

{
"Response": "Success",
"Message": "",
"HasWarning": false,
"Type": 100,
"RateLimit": {},
"Data": {
"Aggregated": false,
"TimeFrom": 1234567800,
"TimeTo": 1234567900,
"Data": [
{
"id": 11,
"symbol": "AAA",
"time": 1234567800,
"block_time": 123.282828282828,
"block_size": 1212121,
"current_supply": 10101010
},
{
"id": 12,
"symbol": "BBB",
"time": 1234567900,
"block_time": 234.696969696969,
"block_size": 1313131,
"current_supply": 20202020
},

]
}
}

What I have tried:

Tried using json_normalize() , flatten module as well. But looking for a generic function which would be able to convert any nested JSON file to CSV.

But json_normalize and flaten modules only provide a single row at the end with all the column data in it.
Looking for a all column data in a tabular format { not only for this example } , but if we can have such Python function to generically convert a nested JSON file to CSV, that will be very helpful
Posted
Updated 22-Feb-22 6:37am

1 solution

Try some of these suggestions from JSON file to CSV python - Google Search[^].
 
Share this answer
 
Comments
Eja07 26-Feb-22 11:16am    
Tried this code, but its gives our error as :: "ValueError: Length of values (10) does not match length of index (1)"

Searched for solution , seems we have to convert list here to series. How should we add the same in below shared code ::


import json
import pandas


def read_json(filename: str) -> dict:
try:
with open(filename, "r") as f:
data = json.loads(f.read())
except:
raise Exception(f"Reading {filename} file encountered an error")

return data


def normalize_json(data: dict) -> dict:
new_data = dict()
for key, value in data.items():
if not isinstance(value, dict):
new_data[key] = value
else:
for k, v in value.items():
new_data[key + "_" + k] = v

return new_data


def main():
# Read the JSON file as python dictionary
data = read_json(filename=r"article.json")

# Normalize the nested python dict
new_data = normalize_json(data=data)

print("New dict:", new_data, "\n")

# Create a pandas dataframe
dataframe = pandas.DataFrame(new_data, index=[0])

# Write to a CSV file
#dataframe.to_csv(r"article.csv")


if __name__ == '__main__':
main()
Richard MacCutchan 26-Feb-22 11:19am    
Where does that error occur, and what data is it trying to use?
Eja07 27-Feb-22 4:21am    
Error occurs at this line "dataframe = pandas.DataFrame(new_data, index=[0])", and sample data is already shared above.
Richard MacCutchan 27-Feb-22 6:46am    
The documentation at pandas.DataFrame — pandas 1.4.1 documentation[^] is not completely clear. But it implies that the index needs to be the same as the number of values in the first parameter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900