I am having issues with converting csc to nested json. I read similar topics here, but unfortunately was not able to move later:( Thanks much for your advices. The simplified csv looks like:
And the desired json should be as follows:
{"data_type": 1,"person": {"id":1,"name": "Anne","surname": "Frank"},
{"data_type": 2,"address": {"id":1,"street": "de Bruijn","city": "Amsterdam"}
{"data_type": 1,"person": {"id":2,"name": "Terry","surname": "Gilliam"},
{"data_type": 2,"address": {"id":2,"street": "Westminster","city": "London"}
I am using pandas and I am struggling with following issues:
Loop over each row and convert them according to your format:
cols = ['id', 'name', 'surname', 'street', 'city']
make_dict = lambda row: {
'data_type': row['data_type'],
row['data_name']: row[cols].dropna().to_dict()
}
df.apply(make_dict, axis=1).to_list()
Result:
[
{
"data_type": 1,
"person": {"id": 1, "name": "Anne", "surname": "Frank"}
},
{
"data_type": 2,
"address": {"id": 1, "street": "de Brujin", "city": "Amsterdam"}
},
{
"data_type": 1,
"person": {"id": 2, "name": "Terry", "surname": "Gilliam"}
},
{
"data_type": 2,
"address": {"id": 2, "street": "Westminster", "city": "London"}
}
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.