0

I am trying to compare two .json files using

# Compare two files 
$BLF = (Get-Content -Path C:\Users\...\Documents\Android1.json)
$CLF =  (Get-Content -Path C:\Users\...\Documents\Android2.json)

$aUnsorted = Compare-Object -ReferenceObject $BLF -DifferenceObject $CLF -IncludeEqual 

I have written code to compare regular variables such as the below "passcodeBlockSimple", and register the value in one file vs the other. I would like to do the same with values that are defined inside brackets:

"variableName": [ 
                     "99"
                ]

I have a solution for when the two files have the same value, by adding onto the value until it finds the end-bracket ]. That gives "variableName": ["99"]. See the variable "roleScopeTagIds" below as an example of such a variable.

I would like to find the value when they are different, but the output from Compare-Object lists the difference line-by-line. Example below, where I loose the information about the variable "restrictedApps" when it is different in the two files (File1 has value "1" and File2 has Value "2").

InputObject                                               SideIndicator
-----------                                               -------------
"@odata.type":  "#microsoft.graph.iosCompliancePolicy",   ==           
    "roleScopeTagIds":  [                                 ==           
                            "0"                           ==           
                        ],                                ==           
    "version":  1,                                        ==           
    "passcodeMinutesOfInactivityBeforeScreenTimeout":  1, ==           
    "managedEmailProfileRequired":  false,                ==           
    "restrictedApps":  [                                  ==           
                       ],                                 ==           
        "passcodeBlockSimple":  false,                    =>           
                        "2"                               =>           
"passcodeBlockSimple":  true,                             <=           
                        "1"                               <= 

The files are, File 1:

"@odata.type":  "#microsoft.graph.iosCompliancePolicy",
    "roleScopeTagIds":  [
                            "0"
                        ],
    "version":  1,
    "passcodeBlockSimple":  true,
    "passcodeMinutesOfInactivityBeforeScreenTimeout":  1,
    "managedEmailProfileRequired":  false,
    "restrictedApps":  [
                            "1"
                       ],

And File 2:

"@odata.type":  "#microsoft.graph.iosCompliancePolicy",
    "roleScopeTagIds":  [
                            "0"
                        ],
    "version":  1,
    "passcodeBlockSimple":  false,
    "passcodeMinutesOfInactivityBeforeScreenTimeout":  1,
    "managedEmailProfileRequired":  false,
    "restrictedApps":  [
                            "2"
                       ],

So my question is: How can i find the value of the variable when they are different, and the Compare-Object result is as above?

(I have to use the Compare-Object function)

esr
  • 1
  • Check out examples four and five of [`Compare-Object`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-7) using the `-Property` parameter. You will want to convert your strings to a PSObject first with [`ConvertFrom-Json`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7). Ensure that the strings are well formed for .NET. – Ash Jul 06 '20 at 10:10
  • Do I convert before or after the comparison? And also, there is no property that I know that separates the name and the value of the variable. They are all in the Value – esr Jul 06 '20 at 10:49
  • Before the comparison. You should compare the converted objects. In your example above, I would set `roleScopeTagIds` and `restrictedApps` as the `-Property` value. – Ash Jul 06 '20 at 11:57

1 Answers1

1

I dont get it totally, becuase youre jsons are invalid, but this code should resolve your issue:

Json1:

{
"@odata.type":  "#microsoft.graph.iosCompliancePolicy",
    "roleScopeTagIds":  [
                            "0"
                        ],
    "version":  1,
    "passcodeBlockSimple":  true,
    "passcodeMinutesOfInactivityBeforeScreenTimeout":  1,
    "managedEmailProfileRequired":  false,
    "restrictedApps":  [
                            "1"
                       ]
}

Json2:

{
"@odata.type":  "#microsoft.graph.iosCompliancePolicy",
    "roleScopeTagIds":  [
                            "0"
                        ],
    "version":  1,
    "passcodeBlockSimple":  false,
    "passcodeMinutesOfInactivityBeforeScreenTimeout":  1,
    "managedEmailProfileRequired":  false,
    "restrictedApps":  [
                            "2"
                       ]
}
[Array]$FileSoll = Get-Content C:\Users\Alex\Desktop\1.json
[Array]$FileIst = Get-Content C:\Users\Alex\Desktop\2.json

    $FileSoll = $($FileSoll | ConvertFrom-Json | ConvertTo-Json) -split ([Environment]::NewLine)
    $FileIst = $($FileIst | ConvertFrom-Json | ConvertTo-Json) -split ([Environment]::NewLine)
    $diff = Compare-Object -ReferenceObject $FileSoll -DifferenceObject $FileIst

Returns the differences:

InputObject                        SideIndicator
-----------                        -------------
    "passcodeBlockSimple":  false, =>
                           "2"     =>
    "passcodeBlockSimple":  true,  <=
                           "1"     <=
Farbkreis
  • 604
  • 3
  • 12