0

I have two json objects. Both may have common keys, Some present only in first object, while some present only in second. When I come these two objects the structure of objects should not change. Only a new level of keys will be added such as the specifying for which object that value belongs to. Even the structure of json objects represent a hierarchy whose levels are not fixed.

a = {"parent1" : { "childa1" : { "grandchilda11" : { "data": values}, 
                                 "grandchilda12" :{"data" : values }
                                }
                               "data" : values 
                 }
                 { "childa2" : { "grandchilda21" : { "data": values}, 
                                 "grandchilda22" :{"data" : values }
                                }
                               "data" : values 
                 }                  
     }
b = { "parent1" : { "childa1" : { "grandchilda11" : { "data": values}, 
                             "grandchilda12" :{"data" : values }
                            }
                           "data" : values 
             }
             { "childa2" : { "grandchilda21" : { "data": values}, 
                             "grandchilda22" :{"data" : values }
                            }
                           "data" : values 
             },
     "parent2" : { "childb1" : { "grandchildb11" : { "data": values}, 
                             "grandchildb12" :{"data" : values }
                            }
                           "data" : values 
             }
             { "childb2" : { "grandchildb21" : { "data": values} 

                            }
                           "data" : values 
             }

 }

The resultant should combine this type of json string . The data level here contains directly values while in the resultant it should be like which object it belongs acting as key and then values .

 "data" : { "a" : values , "b":values}
2
  • It's not entirely clear to me what values is supposed to be: the whole data dictionary, or perhaps the already existing 'data' key? If the latter, what is the expected behavior? Flattening the values? Commented Oct 20, 2016 at 11:07
  • 3
    You don't have "two json objects", you have two dict. Plain old Python dicts. Most used type in the language, very well documented. Now could you please edit your question with at least 1. a complete example of inputs and expected outputs for these inputs ("values" is not a complete example) and 2. what you tried that didn't work ? Commented Oct 20, 2016 at 11:09

2 Answers 2

1

I think this may be what you're looking for.

If you have data dictionaries a and b you can call the below function using merge(a, b) to merge the two like you've said. It does rely on the assumption that both dictionaries have the same structure to them.

def merge(a, b):
    if type(a) is str:
        return { 'a': a, 'b': b }

    else:
        for key in a:
            a[key] = merge(a[key], b[key])

    return a
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

def merge(obj1, obj2):
for key in obj2:
    obj1[key] = obj2[key]
return obj1

Comments

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.