I have a JSON like this for example:
[
{
'name':'test1',
'random1':'random_1_value'
},
{
'name':'test2',
'random2':'random_2_value'
'random4':'random_4_value'
},
{
'name':'test3',
'random3':'random_3_value'
},
]
I want to convert this JSON and construct CSV header based on dictionary keys. Then fill each row respectively. Expected output:
name, random1, random2, random4, random3
test1, random_1_value
test2, ,random_2_value, random_4_value, ,
test3, , , , random_3_value
This is my code so far:
data = json.loads(open('output_data.json').read())
csvwriter = csv.writer(open("output.csv", "w"))
count = 0
for emp in data:
if count == 0:
header = emp.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(emp.values())