I have JSON data and I would like to covert it to CSV
I've tried following this How can I convert JSON to CSV? but it still doesn't work.
I know I need to break the nested objects in JSON to be put in CSV.
Another thing I am concern about is I want to have a loop where multiple items (regardless of the number) can pass through the code as my "ITEM" variable may be >1
this is my JSON sample
data_dict =
[{'count': 19804,
'next': {'limit': 1, 'offset': 1},
'previous': None,
'results': [{'company_name': 'Sunshine and Flowers',
'delivery_address': '34 olive beach house, #01-22, 612345',
'delivery_timeslot': {'bounds': '[)',
'items': [{'id': 21668,
'metadata': {},
'name': 'Loose hair flowers',
'quantity': 1,
'removed': None},
{'id': 21667,
'metadata': {},
'name': "Groom's Boutonniere",
'quantity': 1,
'removed': None},
{'id': 21666,
'metadata': {},
'name': 'Bridal Bouquet',
'quantity': 1,
'removed': None}],
'lower': '2019-12-06T10:00:00Z',
'upper': '2019-12-06T13:00:00Z'}}]}]
this is what I've tried so far
import csv
import json
dict_data = json.loads(dict_data)
f = csv.writer(open("test.csv", "wb+"))
for dict_data in dict_data:
f.writerow([dict_data["count"],
dict_data["next"]["limit"],
dict_data["next"]["offset"],
dict_data["results"]["company_name"],
dict_data["results"]["delivery_address"],
dict_data["results"]["delivery_timeslot"]["lower"]["'upper"]["'bounds"],
dict_data["results"]["items"]["id"]["name"]["'quantity"]["metadata"]["removed"]])
the error message I am getting is this
'not {!r}'.format(s.__class__.__name__)) TypeError: the JSON object must be str, bytes or bytearray, not 'list'