0

I have a JSON file named "Dev-Env-VNET-vnet-details.json" with the following details below,

  {
        "location": "southeastasia",
        "name": "Dev-Env-VNET",
        "properties": {
            "addressSpace": {
                "addressPrefixes": [
                    "10.0.0.0/16",
                    "172.16.0.0/16",
                    "192.168.1.0/24"
                ]
            },
            "enableDdosProtection": false,
            "provisioningState": "Succeeded",
            "subnets": [
                {
                    "name": "default",
                    "properties": {
                        "addressPrefix": "10.0.0.0/24",
                        "delegations": [],
                        "privateEndpointNetworkPolicies": "Enabled",
                        "privateLinkServiceNetworkPolicies": "Enabled",
                        "provisioningState": "Succeeded"
                    },
                    "resourceGroup": "Dev-Env",
                    "type": "Microsoft.Network/virtualNetworks/subnets"
                },
                {
                    "name": "Dev-LAN",
                    "properties": {
                        "addressPrefix": "172.16.0.0/24",
                        "delegations": [],
                        "privateEndpointNetworkPolicies": "Enabled",
                        "privateLinkServiceNetworkPolicies": "Enabled",
                        "provisioningState": "Succeeded",
                        "serviceEndpoints": [
                            {
                                "locations": [
                                    "*"
                                ],
                                "provisioningState": "Succeeded",
                                "service": "Microsoft.AzureCosmosDB"
                            },
                            {
                                "locations": [
                                    "southeastasia"
                                ],
                                "provisioningState": "Succeeded",
                                "service": "Microsoft.Sql"
                            },
                            {
                                "locations": [
                                    "southeastasia",
                                    "eastasia"
                                ],
                                "provisioningState": "Succeeded",
                                "service": "Microsoft.Storage"
                            },
                            {
                                "locations": [
                                    "*"
                                ],
                                "provisioningState": "Succeeded",
                                "service": "Microsoft.KeyVault"
                            }
                        ]
                    },
                    "resourceGroup": "Dev-Env",
                    "type": "Microsoft.Network/virtualNetworks/subnets"
                }
            ]
        },
        "resourceGroup": "Dev-Env",
        "tags": {
            "Environment": "Dev"
        },
        "type": "Microsoft.Network/virtualNetworks"
    }

My goal is to return all the names and addressPrefixes under the subnet array in a much readable format. I can already get the name but I have no idea and having a hard time getting the addressPrefixes since it's under properties

Here's my code,

import json

vnet_data = open('.\Dev-Env-VNET-vnet-details.json')
vnet_json = json.load(vnet_data)

get_subnets = vnet_json['properties']
subnet = get_subnets['subnets']
for s in range(len(subnet)):
    print("Subnet name: {}, addressPrefix: {} ".format(subnet[s]["name"],subnet[s]["properties"]))

And here's the result,

Subnet name: default, addressPrefix: {'addressPrefix': '10.0.0.0/24', 'delegations': [], 'privateEndpointNetworkPolicies': 'Enabled', 'privateLinkServiceNetworkPolicies': 'Enabled', 'provisioningState': 'Succeeded'} 
Subnet name: Dev-LAN, addressPrefix: {'addressPrefix': '172.16.0.0/24', 'delegations': [], 'privateEndpointNetworkPolicies': 'Enabled', 'privateLinkServiceNetworkPolicies': 'Enabled', 'provisioningState': 'Succeeded', 'serviceEndpoints': [{'locations': ['*'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.AzureCosmosDB'}, {'locations': ['southeastasia'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.Sql'}, {'locations': ['southeastasia', 'eastasia'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.Storage'}, {'locations': ['*'], 'provisioningState': 'Succeeded', 'service': 'Microsoft.KeyVault'}]}

Here's what I'm expecting or trying to achieve,

Subnet name: default, addressPrefix: 10.0.0.0/24
Subnet name: Dev-LAN, addressPrefix: 172.16.0.0/24

How am I going to achieve this? Thanks!

2
  • and the issue you are facing is ... ? Commented Oct 7, 2021 at 14:05
  • I have updated the question, sorry about that.. Commented Oct 7, 2021 at 14:12

1 Answer 1

2

In your code:

# Instead of 
#for s in range(len(subnet)):
#    print("Subnet name: {}, addressPrefix: {} ".format(subnet[s]["name"],subnet[s]["properties"]))

for s in subnet:
    print("Subnet name: {}, addressPrefix: {} ".format(
        s["name"], s["properties"]["addressPrefix"]))
Sign up to request clarification or add additional context in comments.

1 Comment

@user3458035 Still advise you to search and read official document carefully, it is quite clear, practical and handy. Mapping Types — dict and JSON encoder and decoder

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.