Python - Getting the intersection of two Json files

I am looking for an opportunity to calculate the intersection of two JSON files. I searched for it and found that I could use the kits for my problem. This works "well." But I need to get a more detailed overview of the intersection. And here the problems begin.

How do I calculate the intersection:

def calcIntersect(ValidationFile, json_object1, json_object2):

with open(ValidationFile) as schema_file:
    schema = j.load(schema_file)
    js.Draft4Validator.check_schema(schema)

with open(json_object1) as spec_file:
    spec1 = j.load(spec_file, object_pairs_hook=OrderedDict)
    js.validate(spec1, schema)

with open(json_object2) as spec_file:
    spec2 = j.load(spec_file, object_pairs_hook=OrderedDict)
    js.validate(spec2, schema)

x = set(spec1) & set(spec2)

print(x)

Data Example 1:

{
    "Car":{
        "Brand":"Audi",
        "Nationality":"Germany",
        "Modelname":"A6"
    },
    "Engine":{
        "cubic capacity":"2967",
        "Enginetype":"V6",
        "Fuel":"Diesel",
        "MaxSpeed":"250"

    },
    "Colors":{
        "Carcolor":"Black",
        "Interiorrcolor":"white"
    }
}

Data Example 2:

{
    "Car":{
        "Brand":"Audi",
        "Nationality":"USA",
        "Modelname":"A6"
    },
    "Engine":{
        "cubic capacity":"2995",
        "Enginetype":"V6",
        "Fuel":"Petrol",
        "MaxSpeed":"250"

    },
    "Colors":{
        "Carcolor":"Black",
        "Interiorrcolor":"Black"
    }
}

Example output:

{'Car', 'Colors', 'Engine'}

These are just Keys, but I need dictators. At the moment, it gives me these keys to say that there is an intersection in it. Perhaps in the “Car” in both files there is an “Audi”, and the nationality is different from the fact that one car is made in America and the other car is made in Germany. But still he returns the “Car”, and not “Audi”.

, . ..

+4
3

, @likeon, , , , .

intersect = { key: [o, spec2[key]] for key, o in spec1.iteritems()
                                   if key in spec2 };

Edit: python 3, items iteritems:

intersect = { key: [o, spec2[key]] for key, o in spec1.items()
                                   if key in spec2 };
+2

spec1 spec2 :

x = {k: v for k, v in spec1.iteritems() if k in spec2 and spec2[k] == v}
+1

You will need a recursive solution:

json1 = {
    "Car": {
        "Brand": "Audi",
        "Nationality": "Germany",
        "Modelname": "A6"
    },
    "Engine": {
        "cubic capacity": "2967",
        "Enginetype": "V6",
        "Fuel": "Diesel",
        "MaxSpeed": "250"
    },
    "Colors": {
        "Carcolor": "Black",
        "Interiorrcolor": "white"
    }
}

json2 = {
    "Car": {
        "Brand": "Audi",
        "Nationality": "USA",
        "Modelname": "A6"
    },
    "Engine": {
        "cubic capacity": "2995",
        "Enginetype": "V6",
        "Fuel": "Petrol",
        "MaxSpeed": "250"
    },
    "Colors": {
        "Carcolor": "Black",
        "Interiorrcolor": "Black"
    }
}


def common_dict(d1, d2):
    output = {}
    for k in set(d1) & set(d2):
        o1, o2 = d1[k], d2[k]
        if isinstance(o1, dict) and isinstance(o2, dict):
            output[k] = common_dict(o1, o2)
        elif o1 == o2:
            output[k] = o1
    return output

print common_dict(json1, json2)
# {'Engine': {'MaxSpeed': '250', 'Enginetype': 'V6'}, 'Car': {'Brand': 'Audi', 'Modelname': 'A6'}, 'Colors': {'Carcolor': 'Black'}}
0
source

Source: https://habr.com/ru/post/1605890/


All Articles