I am just trying to convert simple Json into CSV making it like a table format, so I can easily load them into my database.
I am trying to create some generic code to parse some Json with different metadata, so I hope I don't have to specify the column name and instead hoping Python will generate the column name itself. just like this Json
[
{
"name":"mike",
"sal":"1000",
"dept":"IT",
},
{
"name":"Joe",
"sal":"1200",
"dept":"IT",
}
]
to make it format like this:
name sal dept
Mike 1000 IT
Joe 1200 IT
I use the below code but it doesn't work
import json
import csv
infile = open(r'c:\test\test.json', 'r')
outfile = open(r'c:\test\test.csv', 'w')
writer = csv.writer(outfile)
for row in json.loads(infile.read()):
writer.writerows(row)
Can someone show me some sample code to do this?
Thanks