[Solved] Fetching specific dict values via Python script/code


You have dictionary with list so you will need for-loop (or indexes ie. [0]) to get values.

And because you have nested dictionares so you will have to use all keys to get nested value.

data = {'Response': {'effective-configuration': {'default-zone-access': 1, 'cfg-action': 0, 'db-max': 1045274, 'db-avail': 1041908, 'db-committed': 1268, 'db-transaction': 0, 'db-chassis-wide-committed': 2280, 'transaction-token': 0, 'checksum': 'f9676703bba1f3ac726de1445de27726', 'cfg-name': 'b238638', 'enabled-zone': [{'zone-name': 'Axel_Dellpr740A', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:6b:08:60:1d:e8', '10:00:00:10:9b:58:31:71']}}, {'zone-name': 'Axel_Dellpr740C', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:1d:e8', '50:06:01:63:08:60:1d:e8', '10:00:00:10:9b:58:31:e3']}}, {'zone-name': 'Axel_Rodge_SPA', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:63:08:64:0f:45']}}, {'zone-name': 'Axel_Rodge_SPA_Test', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:63:08:64:0f:45']}}, {'zone-name': 'Axel_Rodge_SPB', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:1d:e8', '50:06:01:6b:08:60:0f:45']}}, {'zone-name': 'Rodge-Dellpr730H', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:64:0f:45', '50:06:01:6b:08:60:0f:45', '10:00:00:10:9b:26:3f:fb']}}, {'zone-name': 'Rodge_Dellpr730E', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:0f:45', '50:06:01:63:08:64:0f:45', '10:00:00:90:fa:02:4c:c4']}}, {'zone-name': 'dellpr730B_Marge', 'zone-type': 0, 'member-entry': {'entry-name': ['21:00:00:24:ff:17:19:cc', '21:00:00:24:ff:17:19:cd', '50:00:09:75:a8:1d:40:19', '50:00:09:75:a8:1d:40:5d']}}]}}}

#print('[DEBUG] type:', type(data) )
#print('[DEBUG] keys:', data.keys() )

#print('[DEBUG] type:', type(data['Response']) )
#print('[DEBUG] keys:', data['Response'].keys() )

#print('[DEBUG] type:', type(data['Response']['effective-configuration']) )
#print('[DEBUG] keys:', data['Response']['effective-configuration'].keys() )

checksum = data['Response']['effective-configuration']['checksum']
print('checksum:', checksum)

zones = data['Response']['effective-configuration']['enabled-zone']
print('type:', type(zones))

for item in zones:
    print('zone-name:', item['zone-name'])

Result:

checksum: f9676703bba1f3ac726de1445de27726
type: <class 'list'>
zone-name: Axel_Dellpr740A
zone-name: Axel_Dellpr740C
zone-name: Axel_Rodge_SPA
zone-name: Axel_Rodge_SPA_Test
zone-name: Axel_Rodge_SPB
zone-name: Rodge-Dellpr730H
zone-name: Rodge_Dellpr730E
zone-name: dellpr730B_Marge

BTW:

You could use json.dumps(..., indent=...) to format it with indentations to better see what you have in JSON.

If you see {...} then it is (nested) dictionary, if you see [...] then it is (nested) list.

You can also use type(item) to test if you have (nested) dictionary or list.
And for dictionary you can use item.keys() to see keys which you can use on this level.

data = {'Response': {'effective-configuration': {'default-zone-access': 1, 'cfg-action': 0, 'db-max': 1045274, 'db-avail': 1041908, 'db-committed': 1268, 'db-transaction': 0, 'db-chassis-wide-committed': 2280, 'transaction-token': 0, 'checksum': 'f9676703bba1f3ac726de1445de27726', 'cfg-name': 'b238638', 'enabled-zone': [{'zone-name': 'Axel_Dellpr740A', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:6b:08:60:1d:e8', '10:00:00:10:9b:58:31:71']}}, {'zone-name': 'Axel_Dellpr740C', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:1d:e8', '50:06:01:63:08:60:1d:e8', '10:00:00:10:9b:58:31:e3']}}, {'zone-name': 'Axel_Rodge_SPA', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:63:08:64:0f:45']}}, {'zone-name': 'Axel_Rodge_SPA_Test', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:60:1d:e8', '50:06:01:63:08:64:0f:45']}}, {'zone-name': 'Axel_Rodge_SPB', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:1d:e8', '50:06:01:6b:08:60:0f:45']}}, {'zone-name': 'Rodge-Dellpr730H', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:63:08:64:0f:45', '50:06:01:6b:08:60:0f:45', '10:00:00:10:9b:26:3f:fb']}}, {'zone-name': 'Rodge_Dellpr730E', 'zone-type': 0, 'member-entry': {'entry-name': ['50:06:01:6b:08:60:0f:45', '50:06:01:63:08:64:0f:45', '10:00:00:90:fa:02:4c:c4']}}, {'zone-name': 'dellpr730B_Marge', 'zone-type': 0, 'member-entry': {'entry-name': ['21:00:00:24:ff:17:19:cc', '21:00:00:24:ff:17:19:cd', '50:00:09:75:a8:1d:40:19', '50:00:09:75:a8:1d:40:5d']}}]}}}

import json

text = json.dumps(data, indent=2)
print(text)

Result:

{
  "Response": {
    "effective-configuration": {
      "default-zone-access": 1,
      "cfg-action": 0,
      "db-max": 1045274,
      "db-avail": 1041908,
      "db-committed": 1268,
      "db-transaction": 0,
      "db-chassis-wide-committed": 2280,
      "transaction-token": 0,
      "checksum": "f9676703bba1f3ac726de1445de27726",
      "cfg-name": "b238638",
      "enabled-zone": [
        {
          "zone-name": "Axel_Dellpr740A",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:63:08:60:1d:e8",
              "50:06:01:6b:08:60:1d:e8",
              "10:00:00:10:9b:58:31:71"
            ]
          }
        },
        {
          "zone-name": "Axel_Dellpr740C",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:6b:08:60:1d:e8",
              "50:06:01:63:08:60:1d:e8",
              "10:00:00:10:9b:58:31:e3"
            ]
          }
        },
        {
          "zone-name": "Axel_Rodge_SPA",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:63:08:60:1d:e8",
              "50:06:01:63:08:64:0f:45"
            ]
          }
        },
        {
          "zone-name": "Axel_Rodge_SPA_Test",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:63:08:60:1d:e8",
              "50:06:01:63:08:64:0f:45"
            ]
          }
        },
        {
          "zone-name": "Axel_Rodge_SPB",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:6b:08:60:1d:e8",
              "50:06:01:6b:08:60:0f:45"
            ]
          }
        },
        {
          "zone-name": "Rodge-Dellpr730H",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:63:08:64:0f:45",
              "50:06:01:6b:08:60:0f:45",
              "10:00:00:10:9b:26:3f:fb"
            ]
          }
        },
        {
          "zone-name": "Rodge_Dellpr730E",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "50:06:01:6b:08:60:0f:45",
              "50:06:01:63:08:64:0f:45",
              "10:00:00:90:fa:02:4c:c4"
            ]
          }
        },
        {
          "zone-name": "dellpr730B_Marge",
          "zone-type": 0,
          "member-entry": {
            "entry-name": [
              "21:00:00:24:ff:17:19:cc",
              "21:00:00:24:ff:17:19:cd",
              "50:00:09:75:a8:1d:40:19",
              "50:00:09:75:a8:1d:40:5d"
            ]
          }
        }
      ]
    }
  }
}

1

solved Fetching specific dict values via Python script/code