Variable substitution: How to update values in a json file and then save the output of the now substituted file

115 Views Asked by At

I have a script that I want to do variable substitution on a json file.

It takes json file A and json file B and converts it into a dynamic object.

So you end up with something like this:

For Json File A:

Json file A key Json file A value
Age ENTER VALUE
ADDRESS ENTER VALUE

For Json File B:

Json file B key Json file B value
Age 38
ADDRESS 10 example lane

If the keys match then the value from Json File B will be subbed into Json File A where the keys match.

So Json File A should end up looking like:

Json file A key Json file A value
Age 38
ADDRESS 10 example lane

I am able to loop through and find matching keys and set the value of Json File A to the Value from Json File B for that Key.

However, I don't know how to get the new value into the actual json file A

Here are the relevant snippets of code:

$appSettings = Get-Content $JsonFileA -Raw | ConvertFrom-Json | Get-LeafProperty 

        if ($keyAA -eq $keyBB ) {
            write-host "$keyAA and $keyBB match"
            $valueAA=$valueBB
            write-host "new appsettings value is now $valueAA"
            
# Save newly populated jsonfile to a new file
            $appSettings | ConvertTo-Json -Depth 4 | Set-Content "path/to/output/file"
        }

The write-host "new appsettings value is now $appvalue" line shows that it has done what i want and updated the value for Json File A to the value from Json File B for the matching key

But it is not saving the value to the output file. The output file is still just what the original Json File A looks like

FULL CODE for context:

function Get-LeafProperty {
  param([Parameter(ValueFromPipeline)] [object] $InputObject, [string] $NamePath)
  process {   
    if ($null -eq $InputObject -or $InputObject -is [DbNull] -or $InputObject.GetType().IsPrimitive -or $InputObject.GetType() -in [string], [datetime], [datetimeoffset], [decimal], [bigint]) {
      [pscustomobject] @{ NamePath = $NamePath; Value = $InputObject }
    }
    elseif ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [System.Collections.IDictionary]) {
      $i = 0
      foreach ($o in $InputObject) { Get-LeafProperty $o ($NamePath + '[' + $i++ + ']') }
    }
    else { 
      $props = if ($InputObject -is [System.Collections.IDictionary] -or $InputObject -is [System.Collections.IEnumerable]) { $InputObject.GetEnumerator() } else { $InputObject.psobject.properties }
      $sep = '.' * ($NamePath -ne '')
      foreach ($p in $props) {
        Get-LeafProperty $p.Value ($NamePath + $sep + $p.Name)
      }
    }
  }
}

$appSettings = Get-Content $jsonfileA -Raw | ConvertFrom-Json | Get-LeafProperty 

$secrets = Get-Content $jsonfileB -Raw | ConvertFrom-Json | Get-LeafProperty 

$leafProperties | ForEach-Object {
    $keyAA = $_.NamePath
    $valueAA = $_.Value

    $secrets | ForEach-Object {
        $keyBB = $_.NamePath
        $keyBB = $_.Value

        if ($keyAA -eq $keyBB ) {
            write-host "$keyAA and $keyBB match"
            $valueAA=$valueBB
            write-host "new appsettings value is now $valueAA"
            $appSettings | ConvertTo-Json -Depth 4 | Set-Content "path/to/output/file"
        }
    }
}


 
0

There are 0 best solutions below