在python中找到两个json文件之间的区别

时间:2019-05-06 13:49:02

标签: python json python-3.x

我有两个json文件,如下所示。我想找到两者之间的差异,并将差异写入第三个json文件。 我期望应该计算json diff-(B.json-A.json)

A.json

  [{
        "Number": 123,
        "brand": "Ford",
        "model": {
            "Mustang1": "2.64",
            "Mustang2": "3.00",
            "Mustang3": "1.00",
            "Mustang4": "1.64"
        }
    },
    {
        "Number": 321,
        "brand": "Toyota",
        "model": {
            "Camry": "2.64",
            "Prius": "3.00",
            "Corolla": "1.00",
            "Tundra": "1.64"
        }
    },
    {
        "Number": 111,
        "brand": "Honda",
        "model": {
            "Accord": "2.64",
            "Civic": "3.00",
            "Insight": "1.00",
            "Pilot": "1.64"
        }
    },
    {
        "Number": 891,
        "brand": "Ford",
        "model": {
            "Mustang1": "2.64",
            "Mustang8": "3.00",
            "Mustang3": "1.00",
            "Mustang6": "1.64"
        }
    },
    {
        "Number": 745,
        "brand": "Toyota",
        "model": {
            "Camry": "2.64",
            "Sienna": "3.00",
            "4Runner": "1.00",
            "Prius": "1.64"
        }
    },
    {
        "Number": 325,
        "brand": "Honda",
        "model": {
            "Accord": "2.64",
            "Passport": "3.00",
            "HR-V": "1.00",
            "Pilot": "1.64"
        }
    },
    {
        "Number": 745,
        "brand": "Accura",
        "model": {
            "TLX": "2.64",
            "MDX": "3.00"
        }
    },
    {
        "Number": 325,
        "brand": "Accura",
        "model": {
            "TLX": "2.64",
            "MDX": "3.00"
        }
    }
]

B.json

        [{
        "Number": 123,
        "brand": "Ford",
        "model": {
            "Mustang1": "2.64",
            "Mustang2": "3.00",
            "Mustang5": "1.64"
        }
    },
    {
        "Number": 321,
        "brand": "Toyota",
        "model": {
            "Camry": "2.64",
            "Prius1": "3.00",
            "Corolla1": "1.00",
            "Tundra": "1.64"
        }
    },
    {
        "Number": 111,
        "brand": "Honda",
        "model": {
            "Accord1": "2.64",
            "Civic1": "3.00",
            "Insight": "1.00",
            "Pilot": "1.64"
        }
    },
    {
        "Number": 891,
        "brand": "Ford",
        "model": {
            "Mustang1": "2.64",
            "Mustang8": "3.00",
            "Mustang3": "1.00",
            "Mustang6": "1.64"
        }
    },
    {
        "Number": 745,
        "brand": "Toyota",
        "model": {
            "Camry2": "2.64",
            "Sienna2": "3.00",
            "4Runner": "1.00",
            "Prius": "1.64"
        }
    },
    {
        "Number": 325,
        "brand": "Honda",
        "model": {
            "Accord": "2.64",
            "Passport2": "3.00",
            "HR-V2": "1.00",
            "Pilot": "1.64"
        }
    },
    {
        "Number": 745,
        "brand": "Accura",
        "model": {
            "TLX": "2.64",
            "MDX2": "3.00"
        }
    },
    {
        "Number": 325,
        "brand": "Accura",
        "model": {
            "TLX1": "2.64",
            "MDX": "3.00"
        }
    }
]

此打印:

{0: {'model': {'$delete': ['Mustang3', 'Mustang4'],
               'Mustang2': '1.00',
               'Mustang5': '1.64'}},
 1: {'model': {'$delete': ['Prius', 'Corolla'],
               'Corolla1': '1.00',
               'Prius1': '3.00'}},
 2: {'model': {'$delete': ['Accord', 'Civic'],
               'Accord1': '2.64',
               'Civic1': '3.00'}},
 4: {'model': {'$delete': ['Camry', 'Sienna'],
               'Camry2': '2.64',
               'Sienna2': '3.00'}},
 5: {'model': {'$delete': ['Passport', 'HR-V'],
               'HR-V2': '1.00',
               'Passport2': '3.00'}},
 6: {'model': {'$delete': ['MDX'], 'MDX2': '3.00'}},
 7: {'model': {'$delete': ['TLX'], 'TLX1': '2.64'}}}

预期结果:是根据B.json-A.json计算的。它将检查A.json中模型中所有的键,但B.json中不存在的键-按其他键(编号,品牌)分组。

{"Number": 123, "brand": "Ford", 'model': {'Mustang2': '1.00', 'Mustang5': '1.64'}},
{"Number": 321, "brand": "Toyota", 'model': {'Corolla1': '1.00', 'Prius1': '3.00'}},
{"Number": 111, "brand": "Honda", 'model': {'Accord1': '2.64', 'Civic1': '3.00'}},
{"Number": 745, "brand": "Toyota", 'model': {'Camry2': '2.64', 'Sienna2': '3.00'}},
{"Number": 325, "brand": "Honda", 'model': {'HR-V2': '1.00', 'Passport2': '3.00'}},
{"Number": 745, "brand": "Accura", 'model': {'MDX2': '3.00'}},
{"Number": 325, "brand": "Accura", 'model': {'TLX1': '2.64'}}}

2 个答案:

答案 0 :(得分:1)

jsondiff并没有执行您(可能)尝试执行的操作。
如果AB列表不一定具有相同的Numberbrand

res = []
for b in B:
    r = dict(b)
    b_in_A = next((a for a in A if b["Number"] == a["Number"] and b["brand"] == a["brand"]), None)
    if b_in_A:
        r["model"] = {k: v for k, v in r["model"].items() if k not in b_in_A["model"]}
    res.append(r)
res

输出:

[{'Number': 123, 'brand': 'Ford', 'model': {'Mustang5': '1.64'}},
 {'Number': 321,
  'brand': 'Toyota',
  'model': {'Corolla1': '1.00', 'Prius1': '3.00'}},
 {'Number': 111,
  'brand': 'Honda',
  'model': {'Accord1': '2.64', 'Civic1': '3.00'}},
 {'Number': 891, 'brand': 'Ford', 'model': {}},
 {'Number': 745,
  'brand': 'Toyota',
  'model': {'Camry2': '2.64', 'Sienna2': '3.00'}},
 {'Number': 325,
  'brand': 'Honda',
  'model': {'HR-V2': '1.00', 'Passport2': '3.00'}},
 {'Number': 745, 'brand': 'Accura', 'model': {'MDX2': '3.00'}},
 {'Number': 325, 'brand': 'Accura', 'model': {'TLX1': '2.64'}}]

答案 1 :(得分:0)

jsondiff似乎只是与各个json对象进行比较以发现其键之间的差异。您似乎正在尝试仅获取不在两个列表中的json对象的列表。可以通过遍历并检查对象是否在两个列表中来完成此操作:

unique_cars = [car for car in jf if car not in jg]
for car in jg:
    if car not in jf:
        unique_cars.append(car)

可能有一个stdlib处理此类事情。但这就是您想要的逻辑。