0

I have a large set of JSON files, each with a different JSON structure. I somehow need to use python to compare the content in those JSON files and return if they are match or not.

JSON file A; JSON file B; Match 
JSON file B; JSON file C; No Match

Suggestions needed on how to compare content of two json files using python.

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
  • You'll have to clarify what it means for JSON files to have the same structure. There are many possible interpretations and the devil is in the details. – Alex Hall Mar 02 '20 at 15:15
  • Please specify the different structures in order for us to be more helpful. – Piyush Mar 02 '20 at 15:43

2 Answers2

2

You could use jsondiff. Assuming you have already read-in the json files, use the following to get a comparison.

from jsondiff import diff
diff(json_A, json_B)

For reading-in a json file, use this.

import json

with open('data.json') as f:
    data = json.load(f)

See also:

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • @MariamBaloch Please consider **`accepting`** and **`up-voting`** the answer, if you think it helped. – CypherX Mar 02 '20 at 16:12
0

It is possible to check line by line each file. here is your code.

import os

json1 = os.getcwd() + "\\test1.json"
json2 = os.getcwd() + "\\test2.json"

Is_Similar = False;

with open(json1) as f1:
   with open(json2) as f2:
      json1list = f1.read().splitlines()
      json2list = f2.read().splitlines()
      list1length = len(file1list)
      list2length = len(file2list)
      if list1length == list2length:
          for index in range(len(file1list)):
              if json1list[index] == json2list[index]:
                  Is_Similar = False;
                  print(file1list[index] + "==" + json2list[index])
              else:                  
                  Is_Similar = False;
                  print(json1list[index] + "!=" + json2list[index]+" Not-Equel")
        if Is_Similar:
            print("Two file is similar ")
        else
            print("Two file is not similar ")
      else:
          print("difference in the size of the file and number of lines")