1
votes

my string:

{"properties":{"template":{"contentVersion":"1.0.0.0","$schema":"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources":"{"type":"Microsoft.Network/networkSecurityGroups","name":"[parameters('GroupName')]","apiVersion":"2016-03-30","location":"[resourceGroup().Location]","properties":{"securityRules":["@{name=DenyAll; properties=}"]}}","parameters":"{"GroupName":{"defaultValue":"GroupName","type":"String"}}"},"mode":"Incremental"}}

Powershell mangles it like this when doing invoke-webrequest\restmethod:

\"{\"properties\":{\"template\":{\"contentVersion\":\"1.0.0.0\",\"$schema\":\"https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#\",\"resources\":\"{\"type\":\"Microsoft.Network/networkSecurityGroups\",\"name\":\"[parameters('GroupName')]\",\"apiVersion\":\"2016-03-30\",\"location\":\"[resourceGroup().Location]\",\"properties\":{\"securityRules\":[\"@{name=DenyAll; properties=}\"]}}\",\"parameters\":\"{\"GroupName\":{\"defaultValue\":\"GroupName\",\"type\":\"String\"}}\"},\"mode\":\"Incremental\"}}\"

Which makes Azure really unhappy. How to avoid that?

my call:

iwr $url -Headers $headers -Body $body -Method Put -ContentType 'application/json'

Basically I had to do this:

$(get-content path -Raw -ReadCount 0) | ConvertFrom-Json

and then use that to pass it

1

1 Answers

1
votes

first it would be nice to check two things, the body of the JSON object and verify that the string complies with a JSON structure.

I found errors in your initial string, the formatting that is valid in JSON is:

{"properties":{"template": {"contentVersion": "1.0.0.0","$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#","resources": {"type": "Microsoft.Network/networkSecurityGroups","name": "[parameters('GroupName')]","apiVersion":"2016-03-30","location":"[resourceGroup().Location]","properties": {"securityRules": ["@{name=DenyAll; properties=}"]}},"parameters":{"GroupName":{"defaultValue":"GroupName","type": "String"}}},"mode":"Incremental"}}

This conversion can be done at http://jsonlint.com/ And Now you can escape the strings uf you use any common editor like notepad ++ and join a simple line and finally escape the string in the url

 {\"properties\": {\"template\": {\"contentVersion\": \"1.0.0.0\",\"$schema\": \"https:\/\/schema.management.azure.com\/schemas\/2015-01-01\/deploymentTemplate.json#\",\"resources\": {\"type\": \"Microsoft.Network\/networkSecurityGroups\",\"name\": \"[parameters('GroupName')]\",\"apiVersion\": \"2016-03-30\",\"location\": \"[resourceGroup().Location]\",\"properties\": {\"securityRules\": [\"@{name=DenyAll; properties=}\"]}},\"parameters \": {\"GroupName\": {\"defaultValue\": \"GroupName\",\"type\": \"String\"}}},\"mode \": \"Incremental\"}}

another advice if you need testing tocalling some service use PostMan

and try again with the call. regards.