-1

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":"abc@gmail.com"  
    },
    {  
        "name":"SES_TO_EMAIL",
        "value":"123@gmail.com"  
    }

File 2:

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

    {  
        "name":"SES_FROM_EMAIL",
        "value":"xyz@gmail.com"  
    },
    {  
        "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": "123@gmail.com"
}

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 :)

oguz ismail
  • 1
  • 16
  • 47
  • 69
Bala krishna
  • 519
  • 1
  • 10
  • 24
  • Use Regex to filter the text to your needs. https://regexone.com/ – Aaron Jones Apr 17 '20 at 05:03
  • 2
    What have you tried so far? – kimbo Apr 17 '20 at 05:11
  • Check [this](https://stackoverflow.com/questions/17802076/comparing-two-files-of-jsons-and-resulting-json-difference) out. This might help you. – Jay Apr 17 '20 at 05:23
  • one option is to create a hashset of all name in file1 , Itterate file2 and check if present in hashset if available remove from hashset(or do further processing ) – Vaibhav Apr 17 '20 at 05:25
  • Check https://stackoverflow.com/questions/20850289/comparing-two-json-files-shell-scripting – Sharad Apr 17 '20 at 05:26
  • In Python3 I use [this](https://stackoverflow.com/a/27266178/6352008) answer as it works for nested dictionnaries as well – Plopp Apr 17 '20 at 07:35

2 Answers2

1

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 ...
oguz ismail
  • 1
  • 16
  • 47
  • 69
  • getting error, Cannot index string with string "name" – Bala krishna Apr 17 '20 at 07:30
  • @Bala that means your input samples do not truly represent your actual input. fix them and let me know – oguz ismail Apr 17 '20 at 07:31
  • 1
    I am a bit confused oguz, Assuming my files are file1.json and file2.json. and file content inside is exactly as mentioned in question. can you edit your prog.jq file, please – Bala krishna Apr 17 '20 at 07:40
  • 1
    @Bala JQ won't recognize that format. It should either be like `[{},{},{}]` (an array of objects), or `{}{}{}` (a stream of objects); `{},{},{}` is not a valid JSON value. I updated my answer to work with `{}{}{}` format. – oguz ismail Apr 17 '20 at 07:45
-1
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
Surya Mahadi
  • 274
  • 1
  • 4