0

I have two JSON files and my requirement is to compare them using PowerShell and form the third JSON with the differences.

For e.g. 1st JSON

{
   Name: Mansing
   City: Edinburgh
   Country: Scotland
}

2nd JSON

{
   Name: Mansing
   City: Edinburgh
   State: Lothian
}

I am expecting the third JSON like below.

{
   Name: Mansing
   City: Edinburgh
   State: Lothian
   Country: India
}

I am trying to convert the JSON file to Powershell object using ConvertFrom-Json then want to compare them and form JSON, but I can't find relevant PowerShell commandlet to do so.

$firstFile  = Get-Content "C:\Users\shinde_mn\Desktop\first.JSON" |
              ConvertFrom-Json
$secondFile = Get-Content "C:\Users\shinde_mn\Desktop\second.JSON" |
              ConvertFrom-Json

#$x = $json | ConvertFrom-Json
Write-Host $firstFile

if (Compare-Object $firstFile.PSObject.Properties $secondFile.PSObject.Properties) {
    Write-Host "no go"
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
mansing shinde
  • 445
  • 7
  • 25
  • 1
    Possible duplicate of [Compare the properties of two PsCustomObjects](https://stackoverflow.com/questions/40536621/compare-the-properties-of-two-pscustomobjects) – JosefZ Jun 06 '19 at 11:32
  • Appearently, you also want to merge the objects which means you probably want to compare them only on certain properties, like`Name` or `City` or `Name and City` or... (This in unclear in your question) besides the value `India` doesn't appear in either of the orginal objects for the `Country` property. Where does it come from? Does is concern two single objects or a list of objects? There are a lot of StackOverflow answers on how to join/merge PowerShell Object, see e g.: https://stackoverflow.com/q/1848821/1701026 – iRon Jun 06 '19 at 14:05
  • Possible duplicate of [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/questions/1848821/in-powershell-whats-the-best-way-to-join-two-tables-into-one) – iRon Jun 06 '19 at 14:10

2 Answers2

1

You could do this to merge the two files:

$firstFile  = '{"Name": "Mansing", "City": "Edinburgh", "Country": "Scotland"}' | ConvertFrom-Json
$secondFile = '{"Name": "Mansing", "City": "Edinburgh", "State": "Lothian"}' | ConvertFrom-Json

foreach ($key in $secondFile.PSObject.Properties.Name)
{
    if ($firstFile.PSObject.Properties.Name -notcontains $key)
    {
        $firstFile | Add-Member -Name $key -Value $secondFile.$key -MemberType NoteProperty
    }
}

$firstFile
mhu
  • 17,720
  • 10
  • 62
  • 93
0

You can do this:

$firstFile  = Get-Content "C:\Users\shinde_mn\Desktop\first.JSON" |
ConvertFrom-Json -AsHashTable

That turns your Json into a hash table and you can use the keys.

Axel Andersen
  • 954
  • 1
  • 6
  • 18
  • I don't see -AsHashTable as a parameter of ConvertTo-Json cmdlet. I have cross checked here https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-6 – mansing shinde Jun 06 '19 at 11:08
  • Hmm, only available on Powershell 6.2 Guess you are on Windows Powershell. If you want the best of the new stuff, you can get it here: https://github.com/PowerShell/PowerShell – Axel Andersen Jun 06 '19 at 11:12
  • `-AsHashTable` is a parameter of `ConvertFrom-Json`, ***not*** `ConvertTo-Json` –  Jun 06 '19 at 13:25
  • @LotPings - correct - I have updated the answer. Thanks! – Axel Andersen Jun 06 '19 at 15:52