Click here to Skip to main content
14,241,604 members

Merge two dicts in separate list by id

Member 14489730 asked:

Open original thread
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"]}]}]}]}
Tags: Python, Python3.6, Python3.3

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




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