I have a param section of a Invoke-RestMethod that i have to keep as is, but also add parameters:
$param = @{
...
Body = '{"entry":[{"@name":SOMETEXT,"static":{"member":[ANOTHERTEXT]}}]}'
...
}
Since the entire Body
is in single quotes, any $parameter
added will be treated like a string instead of a normal PowerShell parameter
In this case SOMETEXT
and ANOTHERTEXT
will be $HubAddressObject
and $ArrayList
, respectivly.
How do i make that Body entry work with parameters, and keeping the same structure (this is part of a Panorama box)?
What i would need would be:
Body = '{"entry":[{"@name":$HubAddressObject,"static":{"member":[$ArrayList]}}]}'
Thanks.
I'd recommend using ConvertFrom-Json
/ ConvertTo-Json
for that kind of thing.
To keep it one 1 line, you can use the -Compress
switch.
$params = @{
Body = '{"entry":[{"@name":"SOMETEXT","static":{"member":"[ANOTHERTEXT]"}}]}'
}
# Create a PSObject representation of your JSON
$jsonObj = $Params.body | ConvertFrom-Json
#Modify whatever you want
$jsonObj.entry[0].'@name' = 'NewText'
# Convert the Object back to Json.
$Params.Body = $JsonObj | Convertto-Json -Compress -Depth 4
Json comparison
# Starting off Json
{"entry":[{"@name":"SOMETEXT","static":{"member":"[ANOTHERTEXT]"}}]}
# Modified JSON
{"entry":[{"@name":"NewText","static":{"member":"[ANOTHERTEXT]"}}]}