Combine two JSON by addition python 2.7

问题内容:

I want to combine two JSON which have the same form and increment the differents array which already exists in one JSON :
JSON a :

[
    {
        "productTitle": "Product1",
        "api-activity": {
            "api1": 1
        },
        "totalCalls": 1
    },
    {
        "productTitle": "Product2",
        "api-activity": {
            "api1": 1,
            "api2": 2,
            "api3": 3,
            "api4": 4
        },
        "totalCalls": 10
    }

]

JSON b:

[
    {
        "productTitle": "Product1",
        "api-activity": {
            "api1": 1
        },
        "totalCalls": 1
    },
    {
        "productTitle": "Product2",
        "api-activity": {
            "api1": 1,
            "api2": 2,
            "api3": 3,
            "api4": 4
        },
        "totalCalls": 10
    },
    {
        "productTitle": "Product3",
        "api-activity": {
            "api1": 2
        },
        "totalCalls": 2
    }

]

To obtain something like this :

[
    {
        "productTitle": "Product1",
        "api-activity": {
            "api1": 2
        },
        "totalCalls": 2
    },
    {
        "productTitle": "Product2",
        "api-activity": {
            "api1": 2,
            "api2": 4,
            "api3": 6,
            "api4": 8
        },
        "totalCalls": 20
    },
    {
        "productTitle": "Product3",
        "api-activity": {
            "api1": 2
        },
        "totalCalls": 2
    }

]

I try to combine them using a previous script I used where I compare the existing JSON to a dataList(which here is our second JSON) like this :

import json

with open('testa.json') as json_data:
    json_a = json.load(json_data)

with open('testb.json') as json_data:
    json_b = json.load(json_data)

with open('outputMerge.json', 'w') as f:

    data_list = json_b
    for data in json_a:
        title = data["productTitle"] #get all product title
        exist = False

        for existing_data in data_list: #loop in data_list

            if title in existing_data["productTitle"]:
                exist = True
                existing_data['totalCalls'] += 1
                if data["apiName"] in existing_data["api-activity"]:
                    existing_data["api-activity"][data["apiName"]] += 1
                else:
                    existing_data["api-activity"][data["apiName"]] = 1

        if not exist:
            new = dict()
            new["appType"] = data["appType"]
            new["totalCalls"] = 1
            new["api-activity"] = dict()
            new["api-activity"][data["apiName"]] = 1
            data_list.append(new) #add the current object in data to datalist

    f.write(json.dumps(data_list, indent=4))

but I have an error with the differents keys I use :

Traceback (most recent call last):
File “merge.py”, line 21, in
if data[“apiName”] in existing_data[“api-activity”]:
KeyError: ‘apiName’

Can you help me debugging I thing I’m close or if you have a better solution maybe ?

问题评论:

    
Your JSON does not contain a dict with the key apiName, so of course you get a KeyError: 'apiName' – what did you expect?

原文地址:

https://stackoverflow.com/questions/47755363/combine-two-json-by-addition-python-2-7

Tags:,

添加评论

友情链接:蝴蝶教程