1

How to slice a JSON object in Python I have a JSON object which looks like:

{
  "value": [
    {
      "name": "b648e43a-6fd8-4b70-9981-28942473e0c3",
      "id": "xxxxxxxxxxxxxxxxxxxxxxxx",
      "etag": "W/\"1c08986f-3337-4c06-815e-aab31b4b196d\"",
      "location": "eastus",
      "tags": {
        "type": "aks-slb-managed-outbound-ip"
      },
      "zones": [
        "2",
        "1",
        "3"
      ],
      "properties": {
        "provisioningState": "Succeeded",
        "resourceGuid": "3bd20fd5-ffa3-4321-8309-354c252bec22",
        "ipAddress": "xxxx.xxxx.xxxx.xxx",
        "publicIPAddressVersion": "IPv4",
        "publicIPAllocationMethod": "Static",
        "idleTimeoutInMinutes": 30,
        "ipTags": [],
        "ipConfiguration": {
          "id": "xxxxxxxxxxxxx"
        }
      },
      "type": "Microsoft.Network/publicIPAddresses",
      "sku": {
        "name": "Standard",
        "tier": "Regional"
      }
    },
    {
      "name": "kubernetes-a40f77cc30dd047928d4e7fdf3c20a12",
      "id": "xxxxxxxxxxxxx",
      "etag": "W/\"057c51ec-33a8-4bb6-9e02-50146f55b66c\"",
      "location": "eastus",
      "tags": {
        "kubernetes-cluster-name": "kubernetes",
        "service": "kube-system/ingress-nginx-public-controller"
      },
      "zones": [
        "1",
        "2",
        "3"
      ],
      "properties": {
        "provisioningState": "Succeeded",
        "resourceGuid": "ab07866e-19c1-4542-ab46-27306b7e7652",
        "ipAddress": "xxx.xxx.xxx.xxx",
        "publicIPAddressVersion": "IPv4",
        "publicIPAllocationMethod": "Static",
        "idleTimeoutInMinutes": 4,
        "ipTags": [],
        "ipConfiguration": {
          "id": "xxxxxxx"
        }
      },
      "type": "Microsoft.Network/publicIPAddresses",
      "sku": {
        "name": "Standard",
        "tier": "Regional"
      }
    }
  ]
}

I need to get "ipConfiguration" block. I tried changing the JSON block in Python dict and look for key value pair but "ipConfiguration" block is deep inside another dict. Can somebody help in figuring out how to get out "ipConfiguration" block data into a variable.

S.B
  • 13,077
  • 10
  • 22
  • 49
Umesh417
  • 75
  • 1
  • 8

1 Answers1

1

I have stored your json data in a file & tried to get all ipConfiguration's id in a variable. Please find the code snippet for the same.

Code :-

import json

ipConfiguration_ids = []
with open('sample.json', 'r') as json_file:
    json_dict = json.loads(json_file.read())
    for value in json_dict['value']:
        ipConfiguration_ids.append(
            value['properties']['ipConfiguration']['id'])
print(ipConfiguration_ids)

Output:-

['xxxxxxxxxxxxx', 'xxxxxxx']
Sanjay R B
  • 39
  • 4