1

When I try to access the below JSON file from powershell it show the following error: Missing property name after reference operator. Here is my JSON file:

{
    "Vnet": {
        "Service": "VirtualNetwork",
        "Name": "vnetname",
        "ResourceGroupName": "RGname",
        "Location": "Southeast Asia",
        "Address_Prefix": "11.0.0.0/16",
        "No_of_subnets": "1",
        "Subnet": {
            "1": {
                "SubnetName":"sub1",
                "Address_Prefix":"11.0.1.0/24"
            }
        }
    }
}

Here is the simple powershell command

$file = (Get-Content "//filelocation" | Out-String) | ConvertFrom-Json
Write-Output $file.Vnet.Subnet.1.SubnetName

Note: I'm author of the JSON file and I can make any changes to it too if needed.

2
  • Well, the JSON is 100% valid. Commented Mar 8, 2018 at 10:22
  • Yeah,verified in JSON lint too.You have any idea about the error?and how to solve it Commented Mar 8, 2018 at 10:23

1 Answer 1

7

PowerShell seems to misinterpret the 1 as an index. Put it in quotes (to make clear it's a property name) and the code will work as you expect:

Write-Output $file.Vnet.Subnet.'1'.SubnetName
Sign up to request clarification or add additional context in comments.

1 Comment

Happens with any propertyname that starts with a number. E.g. 30DayLogCount

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.