简体   繁体   中英

Return the value and data inside a JSON Array via Python

I have a JSON file named "Dev-Env-.NET-.net-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 su.net 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM