I am trying to encode json into csv in python with pandas, which is supposed to be easy, but the output isn't close to right. Example json
{'energy': {'timeUnit': 'DAY', 'unit': 'Wh', 'measuredBy': 'INVERTER', 'values': [{'date': '2022-01-01 00:00:00', 'value': 322.0}, {'date': '2022-01-02 00:00:00', 'value': 12.0}, {'date': '2022-01-03 00:00:00', 'value': 0.0}]}}
With the following code:
data = r.json()
print(data)
json_object = json.dumps(data)
json_object
with open(r'\\shared\AppDev\Production\data\solaredge\import.json','w') as n:
n.write(json_object)
df = pd.read_json(r'\\shared\AppDev\Production\data\solaredge\import.json')
df.to_csv(r'\\shared\AppDev\Production\data\solaredge\import.csv', index = None)
Produces
energy INVERTER DAY Wh [{'date': '2022-01-01 00:00:00', 'value': 322.0}, {'date': '2022-01-02 00:00:00', 'value': 12.0}, {'date': '2022-01-03 00:00:00', 'value': 0.0}]
It appears the inner portion of the json hasn't been parsed at all. I'm wondering if I am missing something obvious, I am considering just stripping most of the content out manually with string functions but that seems like there has to be an easier way.
df = pd.read_json(data). There is no need to save it to a file first. See pandas.pydata.org/pandas-docs/version/1.1.3/reference/api/… for more details.