Click here to Skip to main content
14,238,047 members
Rate this:
Please Sign up or sign in to vote.
I'm trying to merge the object based on key specs, most of the keys structure is consistent, taking into consideration the merge will only happen if company_name is the same (in this example, I only have one company_name) and if only (name, {color, type, license, description) are equal across multiple lists.


[
{
    "company_name": "GreekNLC",
    "metadata": [
        {
            "name": "Bob",
            "details": [
                {
                    "color": "black",
                    "type": "bmw",
                    "license": "4DFLK",
                    "specs": [
                        {
                            "properties": [
                                {
                                    "info": [
                                        "sedan",
                                        "germany"
                                    ]
                                },
                                {
                                    "info": [
                                        "drive",
                                        "expensive"
                                    ]
                                }
                            ]
                        }
                    ],
                    "description": "amazing car"
                }
            ]
        },
        {
            "name": "Bob",
            "car_details": [
                {
                    "color": "black",
                    "type": "bmw",
                    "license": "4DFLK",
                    "specs": [
                        {
                            "properties": [
                                {
                                    "info": [
                                        "powerful",
                                        "convertable"
                                    ]
                                },
                                {
                                    "info": [
                                        "drive",
                                        "expensive"
                                    ]
                                }
                            ]
                        }
                    ],
                    "description": "amazing car"
                }
            ]
        }
    ]
}
]


I expect the following output:


<pre lang="Python">[
{
    "company_name": "GreekNLC",
    "metadata": [
        {
            "name": "Bob",
            "details": [
                {
                    "color": "black",
                    "type": "bmw",
                    "license": "4DFLK",
                    "specs": [
                        {
                            "properties": [
                                {
                                    "info": [
                                        "powerful",
                                        "convertable"
                                    ]
                                },
                                {
                                    "info": [
                                        "sedan",
                                        "germany"
                                    ]
                                },
                                {
                                    "info": [
                                        "drive",
                                        "expensive"
                                    ]
                                }
                            ]
                        }
                    ],
                    "description": "amazing car"
                }
            ]
        }
    ]
}
]


What I have tried:

headers = ['color', 'license', 'type', 'description']

def _key(d):
  return [d.get(i) for i in headers]

def get_specs(b):
  _specs = [c['properties'] for i in b for c in i['specs']]
  return [{"properties": [i for b in _specs for i in b]}]

def merge(d):
  new_merged_list = [[a, list(b)] for a, b in groupby(sorted(d, key=_key), key=_key)]
  k = [{**dict(zip(headers, a)), 'specs': get_specs(b)} for a, b in new_merged_list]
  return k

result = {'name': merge(c.get("details")) for i in data for c in i.get("metadata")}

print(json.dumps(result))


but im getting this which is not the output I expect

<pre>{"name": [{"color": "black", "specs": [{"properties": [{"info": 
["amazing", "strong"]}]}]}]}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100