I have a JSON file (sample below) that I need to replace a value in. I will have multiple names to sort through and only need to replace a few values.
{
"name": "[concat(parameters('workspaceName'), '/myLinkedService1')]",
"type": "Microsoft.Synapse/workspaces/linkedServices",
"apiVersion": "2019-06-01-preview",
"properties": {
"annotations": [],
"type": "SqlServer",
"typeProperties": {
"connectionString": "@{type=AzureKeyVaultSecret; store=; secretName=[parameters('superSecretParams')]}"
},
"connectVia": {
"referenceName": "myIR1",
"type": "IntegrationRuntimeReference"
}
},
"dependsOn": [
"[concat(variables('workspaceId'), '/integrationRuntimes/myIR1')]",
"[concat(variables('workspaceId'), '/linkedServices/BI_Synapse_KeyVault')]"
]
}
I tried several scripts to try and replace the referenceName but it seems to only replace the value upon execution of that line but doesn't hold the replaced value in memory so that I can Set-Content in the file.
$jsonFilePath = "C:/Users/me/OneDrive/Desktop/test_file.json"
$jsonData = Get-Content $jsonFilePath -Raw | ConvertFrom-Json
if ($jsonData.name = "[concat(parameters('workspaceName'), '/myLinkedService1')]") {
$jsonData.properties.connectVia.referenceName.Replace("myIR1", "myIR2") # (value is changed properly)
Write-Output $jsonData # (value is already changed back here)
Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath # (doesn't work)
}
Set-Content ($jsonData | ConvertTo-Json -Depth 4) -Path $jsonFilePath # (after running and viewing file, value is still original value)
# This also doesn't work, but I think I'll need to use this ForEach-Object to iterate over all the names.
$jsonFilePath = "C:/Users/me/OneDrive/Desktop/test_file.json"
$jsonData = Get-Content $jsonFilePath -Raw | Out-String | ConvertFrom-Json
ForEach-Object ($name in $jsonData.PSObject.Properties) {
if ($jsonData.name.Contains("[concat(parameters('workspaceName'), '/myLinkedService1')]")) {
$jsonData.properties.connectVia.referenceName.Replace("myIR1", "myIR2")
Write-Output $jsonData.properties.connectVia.referenceName
}
}
$jsonData | ConvertTo-Json -Depth 32 | Set-Content $jsonFilePath
Any ideas? Maybe assign the conditional to a variable and Set-Content on the variable?