Group the data and convert to json data

Viewed 21

I have a data frame with 150 rows and sample two rows mentioned below. Need to convert the data to json data like below.

Input:

artwork_id  creator_id  department_id   art_work    creator department
0   86508   29993   21  {'id': '86508', 'accession_number': '2015.584'...   {'id': '29993', 'role': 'artist', 'description...   {'id': '21', 'name': 'Prints'}
1   86508   68000   21  {'id': '86508', 'accession_number': '2015.584'...   {'id': '68000', 'role': 'printer', 'descriptio...   {'id': '21', 'name': 'Prints'}

desired output: Attached as imageoutput

I have tried using below code

df.groupby(['artwork_id']).agg(lambda x: list(x))
df.to_json(orient = 'records')
1 Answers

Do you get the right format if you do the following:

result = df.to_json(orient="records")

parsed = json.loads(result)

json.dumps(parsed, indent=4)  

or

grouped_art=df.groupby(['artwork_id']).agg(lambda x: list(x))

result = grouped_art.to_json(orient="records")

parsed = json.loads(result)

json.dumps(parsed, indent=4)  
Related