1

I have a JSON file that I am reading into a PowerShell object. I want to be able to replace certain values which I find using the where command and pipes.

[
   {
      "name":"name-1",
      "targets":[
         {
            "attribute":"Country",
            "opt":"In",
            "values":[
               "@country"
            ]
         },
         {
            "attribute":"Environment",
            "opt":"In",
            "values":[
               "@Environment"
            ]
         }
      ],
      "value":{
         "Url":"@url",
         "Version":"@version"
      }
   }
]

I specifically want to replace the what @url, @version, @country and @environment with the values I specify in the powershell.

Setting the value.Url and value.Version seems to work with:

$body=Get-Content -Raw -Path "$path\$filename.json" | ConvertFrom-Json
$body.value.Url=$applicationUrl;
$body.Value.Version = $applicationVersion;

But the targets attribute is a list, so I have to find using the where statement and pipes. Although I can find the correct element using:

$body.Targets | where { $_.attribute -eq "environment" } | Select -First 1 | Select-Object -Property values

All my attempts to set the value have failed, it always remains as is. Powershell is interpreting the object like:

$body.Targets | where { $_.attribute -eq "environment" } | Select -First 1 | Select-Object -Property values


TypeName: Selected.System.Management.Automation.PSCustomObject

Name        MemberType   Definition                     
----        ----------   ----------                     
Equals      Method       bool Equals(System.Object obj) 
GetHashCode Method       int GetHashCode()              
GetType     Method       type GetType()                 
ToString    Method       string ToString()              
values      NoteProperty Object[] values=System.Object[]

How could I set the values property on this object? I just want it to be a string like "qa" or "production"

Thanks

1 Answer 1

1

Try to set the value attribute as follows:

($body.Targets | Where-Object { $_.attribute -eq "environment" } | Select-Object -First 1).values = @("VALUE");
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't this change the values array to a String?
Thanks ,that pointed me in the right direction ($body.Targets | where { $_.attribute -eq "environment" } | Select -First 1).values = @("qa")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.