Home > Enterprise >  Convert JSON to CSV but each json object should be contained in one row
Convert JSON to CSV but each json object should be contained in one row

Time:02-28

I want to convert my json file which has multiple jsons into a csv such that each json is in one column. I don't want to convert it such that each field in json is a seperate column. So there will be only one column and the entire json object is stored as string in it.

Sample JSON file:

[ {"Name" : "abcd","Id" : "123"} , {"Name" : "efgh","Id" : "124"} ] 

Sample csv :

Data
"Name" : "abcd","Id" : "123"
"Name" : "efgh","Id" : "124"

CodePudding user response:

Use:

import pandas as pd
js = [ {"Name" : "abcd","Id" : "123"} , {"Name" : "efgh","Id" : "124"} ] 
df = pd.DataFrame([str(x) for x in js], columns = ['data'])

output:

enter image description here

  • Related