0

I want to search particular keyword from one file to another file, if the keyword in one file doesn't there then it should display that keyword. Both the Json files having different syntax. I want to achieve this through Powershell:

File 1: Data.json

[
    "The request will be processed at a domain controller for domain xxx.COM.",
    "",
    "Group name     ROL_xxx_xxx.NetAppProdAdmins",
    "Comment        Owner: Ramesh (XXXX\\ramesh)",
    "",
    "Members",
    "",
    "-------------------------------------------------------------------------------",
    "RAJESH1                  RAMESH1                  ASHOK12                  ",
    "ABHIJIT                  RITIKA1                  BHUPESH                  ",
    "YASHWAN                  ",
    "The command completed successfully.",
    ""
]

Second File: Data2.json

{
    "Group": {
        "Path": "/xx-life-xxxxx-xxxx/", 
        "CreateDate": "2017-12-08T12:08:39Z", 
        "GroupId": "xxxxxxxxxxxxxxxxxxxxx", 
        "Arn": "arn:aws:iam::123456789012:group/xx-xxxx-xxxxxxx-xxxxxx/xx-xxxx-xxxxxxx-xxxxxx-admin", 
        "GroupName": "xx-xxxx-xxxxxxx-xxxxxx-admin"
    }, 
    "Users": [
        {
            "UserName": "RAJESH1", 
            "PasswordLastUsed": "2020-05-06T07:10:37Z", 
            "CreateDate": "2020-03-27T07:12:44Z", 
            "UserId": "ASDFGHJKLQWERTYUIOZX", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/RAJESH1"
        }, 
        {
            "UserName": "RAMESH1", 
            "PasswordLastUsed": "2020-06-04T07:01:44Z", 
            "CreateDate": "2019-07-25T08:36:28Z", 
            "UserId": "ASDFGHJKLQWERTYUIOZX", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/RAMESH1"
        }, 
        {
            "UserName": "ASHOK12", 
            "PasswordLastUsed": "2020-06-03T13:18:49Z", 
            "CreateDate": "2020-04-20T14:43:59Z", 
            "UserId": "ASDFGHJKLQWERTYUIOZX", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/ASHOK12"
        }, 
        {
            "UserName": "ABHIJIT", 
            "PasswordLastUsed": "2020-06-05T03:20:48Z", 
            "CreateDate": "2019-05-15T09:55:04Z", 
            "UserId": "AIDA5GALAASEO7TUXCB3T", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/ABHIJIT"
        }, 
        {
            "UserName": "RITIKA1", 
            "PasswordLastUsed": "2020-06-02T14:01:11Z", 
            "CreateDate": "2019-08-01T11:13:30Z", 
            "UserId": "AIDA5GALAASEOADBBKJ7N", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/RITIKA1"
        }, 
        {
            "UserName": "BHUPESH", 
            "PasswordLastUsed": "2020-06-01T08:13:14Z", 
            "CreateDate": "2018-07-26T14:11:10Z", 
            "UserId": "AIDAI2A5HABPPLA6PL5AC", 
            "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
            "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/BHUPESH"
        }
    ]
}

So particularly I want to search RAJESH1 from first json to the second Json, if it is there then it's fine if it's not there then it will show RAJESH1 in the output. In above two JSON "YASHWAN" is not present in second JSON so the output needs to come as "YASHWAN".

Thanks in Advance.

0

1 Answer 1

1

For test, I have added an extra user to the Data2.JSON file:

{
    "UserName": "PARIDA", 
    "PasswordLastUsed": "2020-06-01T08:13:14Z", 
    "CreateDate": "2018-07-26T14:11:10Z", 
    "UserId": "AIDAI2A5HABPPLA6PL5AX", 
    "Path": "/xx-xxxx-xxxxxxx-xxxxxx/", 
    "Arn": "arn:aws:iam::123456789012:user/xx-xxxx-xxxxxxx-xxxxxx/PARIDA"
}

to be able to show the result better.

You can compare the members from both files like this:

# read the JSON files
$data1 = Get-Content -Path 'Data.json' -Raw | ConvertFrom-Json
$data2 = Get-Content -Path 'Data2.json' -Raw | ConvertFrom-Json

# get the members from $data1
$members1 = (($data1 -join '' -split '-{2,}')[-1] -split 'The command')[0].Trim() -split '\s+'

# get the members from $data1
$members2 = $data2.Users.UserName

# compare both member arrays
Compare-Object -ReferenceObject $members1 -DifferenceObject $members2

The output will look like

InputObject SideIndicator
----------- -------------
PARIDA      =>           
YASHWAN     <= 

The SideIndicator will show in which file the difference is found, ie

  • => means the member name PARIDA was found in $data2, but not in $data1
  • <= means the member name YASHWAN was found in $data1, but not in $data2
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.