0

I'm trying to run an Azure CLI Command to restore Diagnostics settings for a Firewall and I get the error: Failed to parse string as JSON

Loading the segment of Json from an object:

$metrics = ($fwDiagnostics.metrics) | ConvertTo-Json -Compress

Yields:

{"category":"AllMetrics","enabled":true,"retentionPolicy":{"days":90,"enabled":false},"timeGrain":null}

Which looks fine.

But when I run the following command:

az monitor diagnostic-settings create --name $fwDiagnostics.name --metrics $metrics --workspace $fwDiagnostics.workspaceId --resource $resource.id

I get:

Failed to parse string as JSON: {category:AllMetrics,enabled:true,retentionPolicy:{days:90,enabled:false},timeGrain:null} Error detail: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Which appears to have stripped out any "" around the text elements

Raymondo
  • 483
  • 2
  • 6
  • 20
  • Use an Online JSON checker to validate the JSON. Here is one of many : https://jsonlint.com/?force_isolation=true – jdweng Mar 06 '23 at 13:08
  • 1
    The Json is valid and was created by another Azure CLI command to backup the diagnostics. Trying your link confirmed valid Json, so it is not that – Raymondo Mar 06 '23 at 13:42
  • The sad reality up to PowerShell 7.2.x is that an _extra, manual_ layer of ``\``-escaping of embedded `"` characters is required in arguments passed to _external programs_. This has been fixed in PowerShell 7.3,0 with selective exceptions on Windows, for now _by default_ - though a future version may revert to the old, broken behavior, requiring an _opt-in_ for the fix. See the linked duplicate for details. – mklement0 Mar 06 '23 at 13:43
  • I used `$var.replace('"', '"""')` in the end which one of the answers alluded to. Many thanks – Raymondo Mar 06 '23 at 15:33

0 Answers0