Reputation: 181
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
Upvotes: 1
Views: 75
Reputation: 4109
Try to set the value attribute as follows:
($body.Targets | Where-Object { $_.attribute -eq "environment" } | Select-Object -First 1).values = @("VALUE");
Upvotes: 1