Home > other >  How do I add a JSON element if and only if it does not already exist, using Powershell?
How do I add a JSON element if and only if it does not already exist, using Powershell?

Time:11-23

I have a JSON file I need to edit, conditionally. It may be an empty object: {}

or it may contain other data.

I need to see if the data I'd like to add already exists, and if not, add it to that JSON file.

The content in question looks like this (entire JSON file):

{
    {
    "2020.3.19f1": {
        "version": "2020.3.19f1",
        "location": [
            "C:\\Program Files\\Unity\\Hub\\Editor\\2020.3.19f1\\Editor\\Unity.exe"
        ],
        "manual": true
    }
}

In this case, if "2020.3.19f" does not exist, I need to add that block. I looked at these docs but really, lost. Any tips appreciated: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=powershell-7.2 This seems close but I'm lost on the syntax of checking for null or empty, and how that translates to PS: PowerShell : retrieve JSON object by field value

Edit: So, for example if the original file is:

{}

Then I need to overwrite that file with:

{
    {
    "2020.3.19f1": {
        "version": "2020.3.19f1",
        "location": [
            "C:\\Program Files\\Unity\\Hub\\Editor\\2020.3.19f1\\Editor\\Unity.exe"
        ],
        "manual": true
    }
}

And if the file already contained something, I need to keep that, just add the new block:

{
    {
    "2019.4.13f1": {
        "version": "2019.4.13f1",
        "location": [
            "C:\\Program Files\\Unity\\Hub\\Editor\\2019.3.13f1\\Editor\\Unity.exe"
        ],
        "manual": true
    },
    {
    "2020.3.19f1": {
        "version": "2020.3.19f1",
        "location": [
            "C:\\Program Files\\Unity\\Hub\\Editor\\2020.3.19f1\\Editor\\Unity.exe"
        ],
        "manual": true
    }
}

FWIW: I did find the condition I need:

    $FileContent = Get-Content -Path "C:\Users\me\AppData\Roaming\UnityHub\editors.json" -Raw | ConvertFrom-Json
    if ( ($FileContent | Get-Member -MemberType NoteProperty -Name "2020.3.19f1") -ne $null )
    {
        echo "it exists"
    }
    else
    {
        echo "add it"
# DO SOMETHING HERE TO CREATE AND (OVER)WRITE THE FILE
    }

CodePudding user response:

You can convert the json as object and add properties as you want.

$json = @"
{
    "2020.3.19f2": {
        "version": "2020.3.19f2",
        "location": [
            "C:\\Program Files\\Unity\\Hub\\Editor\\2020.3.19f2\\Editor\\Unity.exe"
        ],
        "manual": true
    }
}
"@

$obj = ConvertFrom-Json $json
if (-not $obj.'2020.3.19f1') {
  Add-Member -InputObject $obj -MemberType NoteProperty -Name '2020.3.19f1' -Value $(
      New-Object PSObject -Property $([ordered]@{
        version = "2020.3.19f1"
        location = @("C:\Program Files\Unity\Hub\Editor\2020.3.19f1\Editor\Unity.exe")
        manual = $true
      })
  ) -Force
  $obj | ConvertTo-Json
}
  • Related