I have a flattened json (flattened using flatten json library). I have to now group them
{ 'persons_0_address_building': '710',
'persons_0_address_coord': '[123, 40]',
'persons_0_address_street': 'Avenue 1',
'persons_0_address_zipcode': '12345',
'persons_0_cuisine': 'Chinese',
'persons_0_grades_0_date': '2014-03-03T00:00:00.000Z',
'persons_0_grades_0_grade': 'A',
'persons_0_grades_0_score_x': 1,
'persons_0_grades_0_score_y': 2,
'persons_0_grades_1_date': '2011-11-23T00:00:00.000Z',
'persons_0_grades_1_grade': 'A',
'persons_0_grades_1_score_x': 11,
'persons_0_grades_1_score_y': 22,
'persons_0_id': '4d322fa8f552',
'persons_0_name': 'Shash'
}
The desired result is as follows.
person_address =
[
{
'building': '710',
'coord': '[123, 40]',
'street': 'Avenue 1',
'zipcode': '12345',
'id': '4d322fa8f552'
}
]
person =
[{
'cuisine': 'Chinese',
'id': '4d322fa8f552',
'name': 'Shash'
}]
The basic idea is after grouping I can load each of the lists into a flat file.
What all I have tried so far:
- Taking each of the key and checking for
_
, sorting them, and taking the result. This approach had lots of issues. - Tried using
defaultdict
but i am not able to even remotely get to this.
Is there any better way to get to this.