Home > Software engineering >  Powershell - reading json file - how to update a value for a field name that isn't unique
Powershell - reading json file - how to update a value for a field name that isn't unique

Time:03-03

I have the following JSON:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "field1": {
      "value": "fillmein"
    },
    "field2": {
      "value": "fillmein"
    },
    "field3": {
      "value": "fillmein"
    }
  }
}

I need to be able to look up each field at different points, and fill them in. For example:

    "field1": {
      "value": "newv-value"
    }

I have the following code so far in my powershell script:

 $parameters = Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20
 Write-Output $parameters.Values
 Write-Output $parameters.Values.psobject.Properties

It's not clear to me how to navigate the object. Both write-output statements return nothing.

I've also tried to use the PSCustomObject, like this:

$parameters = [PSCustomObject] @ Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20

But I've messed up the syntax it seems cuz I'm getting an "unrecognized token" error with the "@".

Any tips would be appreciated.

CodePudding user response:

Updating your JSON should be as simple as this:

$json = Get-Content 'parameters.json' -Raw | ConvertFrom-Json
$json.parameters.field1.value = 'hello'
$json.parameters.field2.value = 'world'
$json | ConvertTo-Json

Result would be:

{
  "$schema": "https://schema.management.azure.com/schemas/....,
  "contentVersion": "1.0.0.0",
  "parameters": {
    "field1": {
      "value": "hello"
    },
    "field2": {
      "value": "world"
    },
    "field3": {
      "value": "fillmein"
    }
  }
}

As for, how to navigate your object, I guess the key would be to use Get-Member:

PS /> $json | Get-Member -MemberType Properties

   TypeName: System.Management.Automation.PSCustomObject

Name           MemberType   Definition
----           ----------   ----------
$schema        NoteProperty string $schema=https://schema.management.azure.com/schemas/...
contentVersion NoteProperty string contentVersion=1.0.0.0
parameters     NoteProperty System.Management.Automation.PSCustomObject paramete...

PS /> $json.parameters | Get-Member -MemberType Properties

   TypeName: System.Management.Automation.PSCustomObject

Name   MemberType   Definition
----   ----------   ----------
field1 NoteProperty System.Management.Automation.PSCustomObject field1=@{value=fillmein}
field2 NoteProperty System.Management.Automation.PSCustomObject field2=@{value=fillmein}
field3 NoteProperty System.Management.Automation.PSCustomObject field3=@{value=fillmein}

PS /> $json.parameters.field1 | Get-Member -MemberType Properties

   TypeName: System.Management.Automation.PSCustomObject

Name  MemberType   Definition
----  ----------   ----------
value NoteProperty string value=fillmein

CodePudding user response:

I believe The ".values.psobject" is unnecessary. Try the following:

$myjson= Get-Content 'parameters.json' -raw | ConvertFrom-Json -Depth 20
$parameters.parameters.filed = "new-value"
ConvertTo-Json $myjson -Depth 20 | set-content .\parameters.json
  • Related