Compare two json files with same name but different values in two files

Question:

I have two json files with content like following :

File1 :

    {
      "name": "SES_ENABLED",
      "value": "true"
    },
    {  
        "name":"SES_ADDRESS",
        "value":"email-xxxxxx.aws.com"    
    },
    {  
        "name":"SES_FROM_EMAIL",
        "value":"[email protected]"  
    },
    {  
        "name":"SES_TO_EMAIL",
        "value":"[email protected]"  
    }

File 2:

   {
      "name": "SES_ENABLED",
      "value": "false"
    },

    {  
        "name":"SES_FROM_EMAIL",
        "value":"[email protected]"  
    },
    {  
        "name":"SES_ADDRESS",
        "value":"emails-xyzyzyz.aws.com"    
    }

In the above two files the name variable will be same but the values are different and the ordering is different and also there is an extra field in file 1

i.e

{
   "name": "SES_TO_EMAIL"
   "value": "[email protected]"
}

From file1 how can i compare file2 for common “name” variables present and also if any field is missing in file2 than file1, how can I get that.

For example:

After comparing file1 to file2 , I need to get output like "name": "SES_TO_EMAIL" is not present in file2.

Any solution will be very useful.

Thanks in Advance 🙂

Asked By: Bala krishna

||

Answers:

def compare(files):
    # store all name into list of list
    names = [[prop['name'] for prop in file] for file in files]
    for i, name in enumerate(names):
        # create a temporary list
        temp_name = names.copy()
        # remove current name in list
        temp_name.pop(i)
        for n in name:
            for j, temp in enumerate(temp_name):
                if not (n in temp): # if name in not present in the other file, print it
                    print('name: {} is not present in file {}'.format(n, (j+1 if j < i else j + 2)))

this is my naive way, we need to store all the names in list and compare every name with that list. to use it simply

import json
# open the first file
with open('file1.json', 'r') as file:
    file1 = json.load(file)
# open the second file
with open('file2.json', 'r') as file:
    file2 = json.load(file)
# then compare them
compare([file1, file2])

the output will be like this

name: SES_TO_EMAIL is not present in file 2
Answered By: Surya Mahadi

Assuming each file contains a stream of objects, a simple program as below would do the trick.

reduce inputs.name as $name ({}; .[input_filename] += [$name])
| (keys_unsorted | combinations(2)) as $pair
| (.[$pair[0]] - .[$pair[1]])[]
| "name: (.) is not present in ($pair[1])"

Invocation:

jq -rnf prog.jq file1 file2 file3 ...
Answered By: oguz ismail
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.