Home > Net >  Return the value and data inside a JSON Array via Python
Return the value and data inside a JSON Array via Python

Time:10-07

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!

CodePudding user response:

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"]))
  • Related