I have this file .json:
{
"topologyTypes": {
"stations": {
"instances": [
{
"@name": "value1",
"@address": "value2"
},
{
"@name": "value3",
"@address": "value4"
}
]
}
},
"agg": {},
"inter": {}
}
I want to add an object like this in topologyType.stations.instances with PowerShell
{
"@name": "value4",
"@adress": "value5"
}
So I tried this following code in PowerShell, but it doesn't work:
$path = "./data.json"
$jsonFile = Get-Content $path -Raw | ConvertFrom-Json
$jsonContent = @"
{
"@name": "value4",
"@adress": "value5"
}
"@
$jsonFile.topologyTypes.stations.instances |
Add-Content -Value (ConvertFrom-Json $jsonContent)
The desired output I would like to get is like this:
{
"topologyTypes": {
"stations": {
"instances": [
{
"@name": "value1",
"@address": "value2"
},
{
"@name": "value3",
"@address": "value4"
},
{
"@name": "value4",
"@address": "value5"
}
]
}
},
"agg": {},
"inter": {}
}
$jsonContentmakes the JSON string invalid. But even if you remove it, you can't insert the new data into the JSON structure by appending to a file. Even the original JSON string you posted is invalid. Please edit your question show a minimal reproducible example of your code, the input, the desired output, and the arror that code gives you.[with[+$jsonContentbefore parsing it