Reputation: 5
I've been on here a few times but cant seem to fix it. I'm trying to covert JSON to a CSV file. I keep on getting this error: raise JSONDecodeError("Expecting value", s, err.value) from None JSONDecodeError: Expecting value. Please help. This is the code I'm trying to use.
import json
import pandas as pd
# load json object
with open('C:/Users/gavin/OneDrive/Desktop/4th Year/DAD project/mass.json', 'r') as f:
d = json.load(f)
multi = []; inner = {}
def recursive_extract(i):
global multi, inner
if type(i) is list:
if len(i) == 1:
for k,v in i[0].items():
if type(v) in [list, dict]:
recursive_extract(v)
else:
inner[k] = v
else:
multi = i
if type(i) is dict:
for k,v in i.items():
if type(v) in [list, dict]:
recursive_extract(v)
else:
inner[k] = v
recursive_extract(d['data'])
data_dict = []
for i in multi:
tmp = inner.copy()
tmp.update(i)
data_dict.append(tmp)
df = pd.DataFrame(data_dict)
df.to_csv('Output.csv')
Update: This is the JSON that I'm trying to turn into a CSV file. I had to delete some records so that stack overflow would let me post my question.
{
"data": [
{
"value": 4070,
"data_year": 1998,
"month_num": 0,
"key": "Unknown"
},
{
"value": 1095,
"data_year": 1992,
"month_num": 0,
"key": "Unknown"
},
{
"value": 11277,
"data_year": 2017,
"month_num": 0,
"key": "Unknown"
},
{
"value": 3481,
"data_year": 1993,
"month_num": 0,
"key": "Unknown"
},
{
"value": 9402,
"data_year": 2013,
"month_num": 0,
"key": "Unknown"
},
{
"value": 473,
"data_year": 1991,
"month_num": 0,
"key": "Unknown"
},
{
"value": 9587,
"data_year": 1998,
"month_num": 0,
"key": "None"
},
{
"value": 244,
"data_year": 1992,
"month_num": 0,
"key": "None"
}
],
"precise_data": []
}
Upvotes: 0
Views: 312
Reputation: 1330
You might be able to use the pandas inbuilt functionality to read a json file to a pandas dataframe
df=pd.read_json("temp.json")
once the file is read , you can save it as a csv file by
df.to_csv('temp.csv' , index = False)
If that doesn't work ,
you could try any one of the following , to read the json according to its structure
data= pd.read_json('Data.json', lines=True)
data= pd.read_json('Data.json', lines=True, orient='records')
data= pd.read_json('Data.json', orient=str)
Upvotes: 1