sample_data = {
"instances": [
{
"a": "1987",
"b": "2001",
"art": {
"009": {
"art_id": "009",
"mm": {
"1002": {
"mm_code": "1002",
"node": {
"6625583": {
"node_id": "6625583",
"node_sc": "186",
"node_si": [
{
"measure": "S",
"chest": 29
},
{
"measure": "M",
"chest": 32
}
]
}
}
}
}
}
}
}
]
}
what do we need?
I have sample_data and i need to convert sample["instances"][0] into following format
sample_data = {
"instances": [
{
"a": "1987",
"b": "2001",
"art_id": "009",
"mm_code": "1002",
"node_id": "6625583",
"node_sc": "186",
"measure": "S",
"chest": 29
},
{
"a": "1987",
"b": "2001",
"art_id": "009",
"mm_code": "1002",
"node_id": "6625583",
"node_sc": "186",
"measure": "M",
"chest": 32
}
]
}
requirements
- Need a very less time-consuming solution
- Multiple "art_id" s or "art" and "mm" nested values are also possible. This means that :
{ "instances":
[
{ "a": "1987", "b": "2001",
"art":
{
001:{--},
002:{--}
}
},
{ "a": "197", "b": "201",
"art":
{
001:{--},
002:{--}
}
},
]
}
And this applies to mm and nodes as well
tried this solution : imran's solution but could not reach the required format. Thank you for all the help incoming
Here is possible solution how you can flatten this dictionary:
Prints: