Home > Back-end >  Compare multiple json files and add common data in new file in python
Compare multiple json files and add common data in new file in python

Time:07-21

I am able to do with two files, I am not sure if I can use any library to find common data in multiple json file.

import json

with open("data/file1.json", "r") as f1:
    file1 = json.loads(f1.read())
with open("data/file2.json", "r") as f2:
    file2 = json.loads(f2.read())

for item in file2:
    if item in file1:
        print(f"Found common: {item}")
        file1.append(item)

print(f"New file1: {file1}")

CodePudding user response:

Shared Items

def shared_items(dict_1, dict_2):
    return {
        key: dict_1[key]
          for key in dict_1
            if key in dict_2 and \
                dict_1[key] == dict_2[key]
    }

Your Code

import json

with open("data/file1.json", "r") as f1:
    file1 = json.loads(f1.read())
with open("data/file2.json", "r") as f2:
    file2 = json.loads(f2.read())

# Load Function

with open("data/shared.json", "w", encoding="utf-8") as file:
    shared = json.dumps(shared_items(file1, file2), indent=2, ensure_ascii=False, sort_keys=False)
    file.write(shared)

print(f"{shared}\n\n\tdata/shared.json Saved!")

Differences

DeepDiff

This is where deepdiff comes in handy. Deepdiff is a powerful python library to compare 2 dictionaries. What makes it powerful is that, during the comparison, deepdiff does not consider the order in which the elements inside the dictionaries are present. Hence, solves the problem.

Let’s see deepdiff in action

# Load Datas
from json import loads

with open("OLD.json", "r") as file:
    old_json = loads(file.read())  # load from old data
with open("NEW.json", "r") as file:
    new_json = loads(file.read())  # load from new data

# Magic Import
from deepdiff import DeepDiff

differences = DeepDiff(old_json, new_json, ignore_order=True) # compare the dictionaries

## if there is any difference, the diff will not be empty.


# Pretty Print
from rich import print

print(differences)

CodePudding user response:

Simple solution

import json
file1 = open("test.json","r") #opening first file with read permission
file2 = open("test1.json","r") #opening second file with read permission

#loading json files as dictionary objects
object1 = json.load(file1) 
object2 = json.load(file2)

final_dict = {} #to store common elements

commonKeys = list(set(object1.keys()).intersection(object2.keys())) #finding common keys

for key in commonKeys:
    if object1[key] == object2[key]: #if key,val matches in both json file 
        final_dict[key] = object1[key] 

outputfile = open("output.json","w") #opening output file with write operation
json.dump(final_dict, outputfile) #saving final_disct to a json file
  • Related