Home > Back-end >  Fill missing keys by comparing example json in python
Fill missing keys by comparing example json in python

Time:06-22

I want merge two dictionaries first over second, meaning the first dictionary doesn't lose its previous values and only the missing key values are added into first.

Original =  {key1: { key2:[{ key3: y1 , key5: z1 , key6 [{key7: n1}]}]}}

Example = {key1: { key2:[{ key3: None , key4: None ,key5: None , key6 [{key7: None,key8:None}]}]}}

def update2(final, first):
 for k, v in first.items():
    if isinstance(v, str):
        final[k] = v
    elif k in final:
        if isinstance(final[k], dict):
            final[k].update2(first[k])
        else:
            final[k] = first[k]
    else:
        final[k] = first[k]

    final_json = update2(Example['key1'], Original['key1'])
    print(final_json)

#returns only Original not appended output

    def update3(right,left):
        d = dict()
        for key,val in left.items():
         d[key]=[val]
        for key,val in right.items():
         if key not in d.keys():
          d[key].append(val)
         else:
          d[key]=[val]

    final_json = update3(Example['key1'], Original['key1'])
    print(final_json) #returns None

Expected output:

{key1: { key2:[{ key3: y1 , key4: None ,key5: z1 , key6 [{key7: n1,key8:None}]}]}}

I have referred many stackoverlfow posts but nothing is working since it has to be iterated at multiple levels.

Set default values according to JSON schema automatically

how to merge two dictionaries based on key but include missing key values in python?

Merge two dictionaries and persist the values of first dictionaries

My goal is to add default values for missing keys from example file.I am beginner to Python.

CodePudding user response:

Try processing it recursively, in the following steps.

  1. determine if the key is present, if not, assign the value directly, if so go to the next step
  2. determine the type of the value
  • Dictionary: recursive call
  • List: iterate through the contents and make a recursive call for each item
  • Other: assign directly if the original dictionary is not None

like this:

def update(orignal, addition):
    for k, v in addition.items():
        if k not in orignal:
            orignal[k] = v
        else:
            if isinstance(v, dict):
                update(orignal[k], v)
            elif isinstance(v, list):
                for i in range(len(v)):
                    update(orignal[k][i], v[i])
            else:
                if not orignal[k]:
                    orignal[k] = v




Original =  {
    "key1": {
        "key2":[
            { "key3": "y1" ,
              "key5": "z1" ,
              "key6": [
                {"key7": "n1"}
              ]
            }
        ]
    }
}

Example = {
    "key1": {
        "key2":[
            {
                "key3": None ,
                "key4": None ,
                "key5": None ,
                "key6": [
                    {"key7": None,"key8":None}
                ]
            }
        ]
    }
}
print("Original: ", Original)
print("Addition: ", Example)
update(Original, Example)
print("Merge: ", Original)

# Original:  {'key1': {'key2': [{'key3': 'y1', 'key5': 'z1', 'key6': [{'key7': 'n1'}]}]}}
# Addition:  {'key1': {'key2': [{'key3': None, 'key4': None, 'key5': None, 'key6': [{'key7': None, 'key8': None}]}]}}
# Merge:  {'key1': {'key2': [{'key3': 'y1', 'key5': 'z1', 'key6': [{'key7': 'n1', 'key8': None}], 'key4': None}]}}


CodePudding user response:

I assumed that there was no list and that it was not desired that a key present in Original but not present in Example be added to Example. I'm not sure this is the answer that you are looking for, but here is a proposal.

def f(base: dict, data: dict):
    keys = base.keys() & data.keys()
    for key in keys:
        if isinstance(base[key], dict):
            f(base[key], data[key])            
        elif base[key] is None:
            base[key] = data[key]

python

>>> data = {"key1": {"key2": {"key3": "y1", "key5": "z1", "key6": {"key7": "n1"}}}}
>>> base = {"key1": {"key2": {"key3": "a1", "key4": None, "key5": None, "key6": {"key7": None, "key8": None}}}}
>>> f(base, data)
>>> print(base)
{'key1': {'key2': {'key3': 'a1', 'key4': None, 'key5': 'z1', 'key6': {'key7': 'n1', 'key8': None}}}}
  • Related