I'm trying to merge 2 json files in Python. Here are the files:
test1.json
{
    "version": "1.0",
    "data": {
        "admin1": {
            "id": "1",
            "location": "NY"
        },
        "admin2": {
                "id": "2",
                "name": "Bob",
                "location": "LA",
                "admin_key": {
                    "adminvalue1": "admin1",
                    "adminvalue2": "admin2"
                }
        },
        "admin3": {
            "name": "john"
        }
    }
}
test2.json
{
    "data": {
        "user1": {
            "name": "jane",
            "phone": "555-666-7777",
            "enail": "jane@jane.com"
        },
        "user2": {
            "location": "LA",
            "id": "5"
        },
        "user3": {
            "description": "user",
            "location": "NY",
            "name": "zoe",
            "phone": "111-222-3333",
            "user_key": {
                "uservalue1": "user1",
                "uservalue2": "user2"
            }
        }
    }
}
I have this code to merge the two files
import json
with open("test1.json", "r") as data1_file:
    data1 = json.load(data1_file)
with open("test2.json", "r") as data2_file:
    data2 = json.load(data2_file)
data1.update(data2)
with open("out.json", "w") as out_file:
    json.dump(data1, out_file, indent=4)
The output I'm getting is this. It only has test2.json contents under "data".
{
    "version": "1.0",
    "data": {
        "user1": {
            "name": "jane",
            "phone": "555-666-7777",
            "enail": "jane@jane.com"
        },
        "user2": {
            "location": "LA",
            "id": "5"
        },
        "user3": {
            "description": "user",
            "location": "NY",
            "name": "zoe",
            "phone": "111-222-3333",
            "user_key": {
                "uservalue1": "user1",
                "uservalue2": "user2"
            }
        }
    }
}
I want the output to have contents of both files under "data" like below
{
    "version": "1.0",
    "data": {
        "admin1": {
            "id": "1",
            "location": "NY"
        },
        "admin2": {
                "id": "2",
                "name": "Bob",
                "location": "LA",
                "admin_key": {
                    "adminvalue1": "admin1",
                    "adminvalue2": "admin2"
                }
        },
        "admin3": {
            "name": "john"
        },
        "user1": {
            "name": "jane",
            "phone": "555-666-7777",
            "enail": "jane@jane.com"
        },
        "user2": {
            "location": "LA",
            "id": "5"
        },
        "user3": {
            "description": "user",
            "location": "NY",
            "name": "zoe",
            "phone": "111-222-3333",
            "user_key": {
                "uservalue1": "user1",
                "uservalue2": "user2"
            }
        }
    }
}
How can I achieve this? Thanks!
 
     
    