Failed to parse string as JSON

Karthika Nair 0 Reputation points
2023-05-08T18:22:09.0633333+00:00

Hi, I have a requirement to create a custom role with the least privileges required to start and stop a Virtual machine in Azure. I tried the below way however I am getting errors. Any Lead is appreciated.

az role definition create --role-definition'{
    "Name": "Virtual Machine Operator",
	"IsCustom": true,
    "Description": "Can start, restart and stop virtual machines.",
    "Actions": [
        "Microsoft.Compute/*/read",
        "Microsoft.Compute/virtualMachines/start/action",
        "Microsoft.Compute/virtualMachines/restart/action",
		"Microsoft.Compute/virtualMachines/deallocate/action"
    ],
    "NotDataActions": [ ],
    "AssignableScopes": ["/subscriptions/MySubscriptionId"]
}'

Error:

Failed to parse string as JSON:

{

Error detail: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

The provided JSON string may have been parsed by the shell.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,297 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Lakshmi, Karthika 5 Reputation points
    2023-05-18T06:40:16.3033333+00:00

    Hello @vipullag-MSFT

    After some attempts and several tests using Azure CLI and different PowerShell modules, I am able to create the custom role using the same script.

    However, couldn’t pinpoint the reason for the issue, since the script was the same all along.

    Thank you Team for the support and assistance.

    1 person found this answer helpful.
    0 comments No comments

  2. shiva patpi 13,146 Reputation points Microsoft Employee
    2023-05-08T20:57:59.14+00:00

    @Karthika Nair

    Please create a json file , enter the contents and pass that json file as a parameter to the command:

    I just tried that step and able to execute the command successfully:

    User's image

    User's image


  3. James Jung 0 Reputation points
    2024-01-09T19:34:13.7966667+00:00

    I ran into the same problem with running a az vm command within the Powershell. I was able to run the command by using bash instead. I guess powershell does something wrong with parsing double quote within a JSON array.

    0 comments No comments

  4. Vladimirs Davidovs 0 Reputation points Microsoft Employee
    2024-05-15T18:00:18.0266667+00:00

    I ran into the same problem with:

    az vm extension set --name 'KeyVaultForLinux' --publisher 'Microsoft.Azure.KeyVault' ....
    

    which has a '--settings' parameter, that expects a 'JSON' string.

    After a bit of experimentation figured out that you have to escape every double quote '"' in the string with the backslash ''. So instead of:

    
    '{"secretsManagementSettings": { "pollingIntervalInS": "3600", "certificateStoreName": "", "certificateStoreLocation": "/cert-directory", "observedCertificates": ["https://keyvault-name.vault.azure.net/secrets/certificate-name"] }}'
    

    you need to provide:

    '{\"secretsManagementSettings\": { \"pollingIntervalInS\": \"3600\", \"certificateStoreName\": \"\", \"certificateStoreLocation\": \"/cert-directory\", \"observedCertificates\": [\"https://keyvault-name.vault.azure.net/secrets/certificate-name\"] }}'
    
    0 comments No comments