1

So I have the following nested object, and I'm trying to merge the object based on key specs. how can I fix it.

Input:

    [
    {
        "person_name": "bob",
        "metadata": [
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "sedan",
                                            "germany"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            },
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "powerful",
                                            "convertable"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            }
        ]
    }
]

Expected output:

   [
    {
        "person_name": "bob",
        "metadata": [
            {
                "name": "car",
                "car_details": [
                    {
                        "color": "black",
                        "type": "bmw",
                        "specs": [
                            {
                                "properties": [
                                    {
                                        "info": [
                                            "powerful",
                                            "convertable"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "sedan",
                                            "germany"
                                        ]
                                    },
                                    {
                                        "info": [
                                            "drive",
                                            "expensive"
                                        ]
                                    }
                                ]
                            }
                        ],
                        "description": "amazing car"
                    }
                ]
            }
        ]
    }
]

this is the code so far: but it doesn't work.

from itertools import groupby
import ast, json
headers = ['color', 'type', 'description']

def _key(d):
  # get the key from a dictionary
  return [d.get(i) for i in headers]


def get_specs(b):
  _specs = [c['properties'] for i in b for c in ast.literal_eval(i['specs'])]
  return json.dumps([{'specs': [i for b in _specs for i in b]}])


def merge(d):
  merged_list = [[a, list(b)] for a, b in groupby(sorted(d, key=_key), key=_key)]
  return [{**dict(zip(headers, a)), 'specs': get_specs(b)} for a, b in merged_list]
5
  • Yeah but that is not the issue here. The indentation Just got messed up when I copied the code to stackoverflow Commented Jun 9, 2019 at 5:45
  • check pypi.org/project/jsonmerge Commented Jun 9, 2019 at 6:52
  • Thanks. This only merges, but does not solve my problem with the code above. Any suggestions on how to fix that Commented Jun 9, 2019 at 16:18
  • @Ajax1234 this is based on your answer today, but still can't figure it out Commented Jun 9, 2019 at 17:22
  • Add what you tried with that ! Commented Jun 10, 2019 at 3:00

1 Answer 1

1
headers = ['color', 'type', 'description']
from pprint import *
from itertools import groupby
def key(x):
    return [x['car_details'][0][i]  for i in headers]
def merge(l):
    b=next(l)
    d=b['car_details'][0]['specs']
    for i in l:
        for j in i['car_details'][0]['specs'][0]['properties']:
            if j not in d[0]['properties']:
                d[0]['properties'].append(j)
    return b
for i in d:    
    t=i['metadata']    
    i['metadata'] =[]
    for k,g in groupby(t,key=key):
        i['metadata'].append(merge(g))

pprint(d)

Output:

 [{'metadata': [{'car_details': [{'color': 'black',
                                 'description': 'amazing car',
                                 'specs': [{'properties': [{'info': ['sedan',
                                                                     'germany']},
                                                           {'info': ['drive',
                                                                     'expensive']},
                                                           {'info': ['powerful',
                                                                     'convertable']}]}],
                                 'type': 'bmw'}],
                'name': 'car'}],
  'person_name': 'bob'}]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, this assumes there is only one object under i['car_details'][0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.