0

Below are my two json file with same kind of records. As you can see it, in one file there are 3 items and in other file there are 5 items. I want those two additional items in another file.

Is there a way to compare two json files and get the additional values in the other file in python?

I found some questions related to comparing two files biut tey justgive the boolean value true or false as the result. Link to the question

===> file a.json

{
  "Count" : 3,
  "Items": [
      {
        "id"    : { "S"  : "1" },
        "name"  : { "S"  : "abc"},
        "email" : { "S"  : "abc@gmail.com"}
      },
        {
        "id"    : { "S"  : "2" },
        "name"  : { "S"  : "def"},
        "email" : { "S"  : "def@gmail.com"}
      },
      {
        "id"    : { "S"  : "3" },
        "name"  : { "S"  : "ghi"},
        "email" : { "S"  : "ghi@gmail.com"}
      }
    ],
  "ScannedCount" : 3
}

===> file b.json

{
  "Count" : 5,
  "Items": [
      {
        "id"    : { "S"  : "1" },
        "name"  : { "S"  : "abc"},
        "email" : { "S"  : "abc@gmail.com"}
      },
        {
        "id"    : { "S"  : "2" },
        "name"  : { "S"  : "def"},
        "email" : { "S"  : "def@gmail.com"}
      },
      {
        "id"    : { "S"  : "3" },
        "name"  : { "S"  : "ghi"},
        "email" : { "S"  : "ghi@gmail.com"}
      },
      {
         "id"    : { "S"  : "4" },
         "name"  : { "S"  : "jkl"},
         "email" : { "S"  : "jkl@gmail.com"}
      },
      {
         "id"    : { "S"  : "5" },
         "name"  : { "S"  : "mno"},
         "email" : { "S"  : "mno@gmail.com"}
      }
    ],
  "ScannedCount" : 5
}
Bhavik Joshi
  • 2,557
  • 6
  • 24
  • 48

2 Answers2

1

What you're looking for is a way to diff JSON blobs. There's several packages out there for this. Here's a few:

Youssef Moussaoui
  • 12,187
  • 2
  • 41
  • 37
1

Create objects from the file representation, compare the items, and then write output to a file.

import json

file_a = json.load(open("./a.json", "r"))
file_b = json.load(open("./b.json", "r"))

items_a = {value["id"]["S"]: value for value in file_a}
items_b = {value["id"]["S"]: value for value in file_b}

diff_keys = set(items_a) ^ set(items_b)
diff_items = {items_a[key] if items_a.has_key(key) else items_b[key] for key in diff_keys}

json.dumps(diff_items, open("file_diff.json", "w"))
Josh Hamet
  • 957
  • 8
  • 10
  • 1
    It gives me following error `File "jsondiff.py", line 6, in items_a = {value["id"]["S"]: value for value in file_a} File "jsondiff.py", line 6, in items_a = {value["id"]["S"]: value for value in file_a} TypeError: string indices must be integers` – Bhavik Joshi Jan 13 '18 at 06:58