Thursday, October 27, 2022

[SOLVED] Compare two json files with same name but different values in two files

Issue

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


Solution

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
Answer Checked By - Marilyn (WPSolving Volunteer)