Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

json diff python

# Assumption - json files are of same format, with same keys

# A function to remove duplicate values
def unique(lst):
    unique_lst = []

    # Iterate over the original list and for each element
    # add it to unique_lst, if its not already there.
    for elem in lst:
        if elem not in unique_lst:
            unique_lst.append(elem)
    return unique_lst


# Using dictionary
def compare_json(json_file_1, json_file_2):
    with open(json_file_1, 'r') as f1, open(json_file_2, 'r') as f2:
        data1 = json.load(f1)
        data2 = json.load(f2)
        key_list = set(data1.keys()).union(set(data2.keys()))
        difference = {key: list(set(data1.get(key, [])).difference(set(data2.get(key, []))))
                      for key in key_list}
        return difference


# Using list
def compare_json_2(json_file_1, json_file_2):
    with open(json_file_1, 'r') as f1, open(json_file_2, 'r') as f2:
        data1 = json.load(f1)
        data2 = json.load(f2)
        #Assuming both json files have same format
        key_list = data1.keys()
        difference = {}
        for key in key_list:
            difference[key] = list(set(data1[key]).difference(set(data2[key])))
            difference[key] = unique(difference[key])
        return difference


print(compare_json('json_1.json', 'json_2.json'))
print(compare_json_2('json_1.json', 'json_2.json'))
Comment

PREVIOUS NEXT
Code Example
Python :: count function in python 
Python :: interfaces in python 
Python :: python all 
Python :: python double underscore methods 
Python :: set() python 
Python :: how to make a label in python 
Python :: concatenate lists 
Python :: python3 -m venv venv 
Python :: python modules list 
Python :: google map distance 
Python :: print column name and index python 
Python :: how to use inputs in python 
Python :: recall calculate confusion matrix .ravel() 
Python :: full body tracking module 
Python :: if lower: --- 71 doc = doc.lower() 72 if accent_function is not None: 73 doc = accent_function(doc) 
Python :: tessellation 
Python :: eastvale roblox python 
Python :: what is a rare earth 
Python :: pandas get indices of mask 
Python :: exception: python in worker has different version 3.7 than that in driver 3.8, pyspark cannot run with different minor versions. please check environment variables pyspark_python and pyspark_driver_python are correctly set. 
Python :: Factory reset the filesystem micropython 
Python :: adding if statements in pyhton with tuple 
Python :: indexers in python 
Python :: df.iterrows write to column 
Python :: limit entries in dataframe column 
Python :: `nlp.add_pipe` now takes the string name of the registered component factory 
Python :: Create an identical list from the first list using list comprehension. 
Python :: reverse the order of list elements 
Python :: repalce na with mean per group 
Python :: dask dataframe csv tutorial 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =