4

I am merging two json in python

I'm doing

import json

json_obj = json.dumps({"a": [1,2]})
json_obj1 = json.dumps({"a": [3,4]})

json_obj += json_obj1

print(json_obj)

I am expecting the output as

{"a": [1, 2,3,4]}

but i got

{"a": [1, 2]}{"a": [3, 4]}

How to get the earlier one?

2
  • 1
    Why where you expecting that output? Those strings. JSON is a text-based serialization format. Commented Feb 5, 2021 at 8:19
  • the solution is to convert both json into a python nested dictionary then join the two dictionaries together on a key Commented Mar 2, 2021 at 23:35

4 Answers 4

6

In json module, dumps convert python object to a string, and loads convert a string into python object. So in your original codes, you just try to concat two json-string. Try to code like this:

import json

from collections import defaultdict


def merge_dict(d1, d2):
    dd = defaultdict(list)

    for d in (d1, d2):
        for key, value in d.items():
            if isinstance(value, list):
                dd[key].extend(value)
            else:
                dd[key].append(value)
    return dict(dd)


if __name__ == '__main__':
    json_str1 = json.dumps({"a": [1, 2]})
    json_str2 = json.dumps({"a": [3, 4]})

    dct1 = json.loads(json_str1)
    dct2 = json.loads(json_str2)
    combined_dct = merge_dict(dct1, dct2)

    json_str3 = json.dumps(combined_dct)

    # {"a": [1, 2, 3, 4]}
    print(json_str3)
Sign up to request clarification or add additional context in comments.

Comments

4

json.dumps() converts a dictionary to str object, not a json(dict) object.

So, adding some dumps statement in your code shows that the type is changed to str after using json.dumps() and with + you are effectively concatenating the two string and hence you get the concatenated output.

Further, to merge the two dictionaries for your simple case, you can just use the append:

import json

json_obj = json.dumps({"a": [1,2]})
json_obj1 = json.dumps({"a": [3,4]})

print(type(json_obj1))  # the type is `str`

json_obj += json_obj1   # this concatenates the two str objects

json_obj = {"a": [1,2]}
json_obj1 = {"a": [3,4]}

json_obj["a"].extend(json_obj1["a"])
 
print(json_obj)

Comments

2

I suggest you to study basic fundamental of Python for your own sake as you don't seem to understand why your code wouldn't work.

import json

# We have two dictionaries to combine
json_obj_1 = {"a": [1,2], "b":[2,3], 'c': [1,2,3]}
json_obj_2 = {"a": [3,4], 'd':[4,2], 'e': [4,2,2]}

Merged dictionary will be stored here

hold_json_obj = {}

Don't worry, it's not actually that complicated. Read the code line by line with comments attached and you'll understand.

# We'll loop through every item in the json_obj_1 dictionary
for item_1 in json_obj_1:
    # We'll also loop through every item in the json_obj_2 dictionary
    for item_2 in json_obj_2:
        # Now let's compare whether they are the same KEYS (not values)
        if item_1 == item_2:
            # if they match, we create a list to store the array
            hold_array = []
            hold_array.extend(json_obj_1[item_1])
            hold_array.extend(json_obj_2[item_1])
            
            # finally putting the array to our hold_json_obj
            hold_json_obj[item_1] = hold_array
        else:
            # if they don't match, check if the key already exists in the
            # hold_json_obj because we might be iterating json_obj_2 for the second time.
            if item_2 not in hold_json_obj:
                #add the ummatched array to hold_json_obj
                hold_json_obj[item_2] = json_obj_2[item_2]

Now simply update json_obj_1 with the update method. The update function is required because if json_obj_1 has keys that json_obj_2 doesn't then we may have missed them out in the above loops.

json_obj_1.update(hold_json_obj)
print(json_obj_1)

This is what the print displays.

{'a': [1, 2, 3, 4], 'b': [2, 3], 'c': [1, 2, 3], 'd': [4, 2], 'e': [4, 2, 2]}

Comments

0

It's important to differentiate between JSON Strings and JSON objects. I use this function to merge 2 complex JSON objects.

def merge_json_objects(obj1, obj2):
    if isinstance(obj1, dict) and isinstance(obj2, dict):
        merged = obj1.copy()
        for key, value in obj2.items():
            if key in merged:
                merged[key] = merge_json_objects(merged[key], value)
            else:
                merged[key] = value
        return merged

    elif isinstance(obj1, list) and isinstance(obj2, list):
        # For lists, extend obj1 with items from obj2 that are not already in obj1
        merged = obj1.copy()
        for item in obj2:
            if item not in merged: #avoid duplicates
                merged.append(item)
        return merged
    
    elif isinstance(obj1, str) and isinstance(obj2, dict) or isinstance(obj2, str) and isinstance(obj1, dict) : # If one is a dict and the other a string, they belong to different places, so we ignore one or the other.
        return obj1
    
    else:
        # Incompatible types, return obj1 by default
        return obj1

Here is how you can use it:

obj1 = {
    "world": {
        "Europe": {
            "France": {
                "1960": {
                    "song1": "path1",
                    "song2": "path2"

                },
                "_unknown":{
                    "song_unknown": "path_unknown"
                }

            }
        }
    }
}

obj2 = {
    "world": {
        "Europe": {
            "France": {
                "1970": {
                    "song3": "path3"
                },
                                "1960": {
                    "song4": "path4"
                }

            },
            "Spain": {
                "1980": {
                    "song5": "path5"
                }
            }
        }
    }
}

merged_object = merge_json_objects(obj1, obj2)

Then, with the resultant JSON Object, you can get the JSON String that represents that object and save that JSON String as a JSON File:

import json
# Get the string with:
json_string = json.dumps(merged_object, indent=4)
# or Save the JSON Object as a JSON String in a JSON File:
with open("myfile.json", "w", encoding="utf-8") as f:
    json.dump(merged_object, f, indent=4)

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.