Skip to content
Advertisement

python read a specific json

I’m trying to read this json. I need values: PoolID, PoolSystemCapacityGb2,NumPagesUsedPool, and others..

But i can only read values for the ‘PooID’:1, not for the PooID’:90

How can i parse this kind of json?

[{'$id': '1', '$type': 'Portal.Data.model.modelXPool, Portal.Data', 'SysId': 1234, 'PoolID': 1, 'PoolKindID': 1, 'PoolRaidLevelMixableID': 1, 'PoolEmulationTypeID': 1, 'PoolSystemCapacityGb2': 637870.75, 'PoolReservedCapacityGb2': 155320.92, 'WarningThreshold': 70, 'SystemThreshold': 80, 'DepletionThreshold': 80, 'ThresholdModeID': 2, 'StatusValue': 1005, 'StatusID': 1, 'StatusDetail': 'Utilization between 0% and Threshold', 'NumOfPoolVolumes': 12, 'MaxPagesOfCapacity': 124, 'NumPagesUsedPool': 1234, 'NumPagesForDpVol': 1234, 'CapacityPerPageKb2': 43008, 'MaxSubscription': 65535, 'DriveInterfaceID': 1, 'DriveSpeed': '', 'DriveInternalExternalID': 1, 'PoolRaidLevelID': 4, 'NumOfTiers': 1, 'PoolDriveInterface': {'$id': '2', '$type': 'Portal.Data.model.modelPoolDriveInterface, Portal.Data', 'ID': 1, 'DisplayText': 'SSD', 'XPool': [{'$ref': '1'}, {'$id': '3', '$type': 'Portal.Data.model.modelXPool, Portal.Data', 'SysId': 1234, 'PoolID': 90, 'PoolKindID': 1, 'PoolRaidLevelMixableID': 1, 'PoolEmulationTypeID': 1, 'PoolSystemCapacityGb2': 36858.77, 'PoolReservedCapacityGb2': 11222.7, 'WarningThreshold': 70, 'SystemThreshold': 80, 'DepletionThreshold': 80, 'ThresholdModeID': 2, 'StatusValue': 1005, 'StatusID': 1, 'StatusDetail': 'Utilization between 0% and Threshold', 'NumOfPoolVolumes': 12, 'MaxPagesOfCapacity': 898652, 'NumPagesUsedPool': 273620, 'NumPagesForDpVol': 898588, 'CapacityPerPageKb2': 43008, 'MaxSubscription': 65535, 'DriveInterfaceID': 1, 'DriveSpeed': '', 'DriveInternalExternalID': 1, 'PoolRaidLevelID': 6, 'NumOfTiers': 1, 'PoolDriveInterface': {'$ref': '2'}, 'Internal': {'$id': '4', '$type': 'Portal.Data.model.modelInternal, Portal.Data', 'ID': 1, 'DisplayText': 'Internal', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XEmulation': {'$id': '5', '$type': 'Portal.Data.model.modelXEmulation, Portal.Data', 'ID': 1, 'DisplayText': 'Open-V', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XParityGroup': []}, 'PoolKind': {'$id': '6', '$type': 'Portal.Data.model.modelPoolKind, Portal.Data', 'ID': 1, 'DisplayText': 'DP', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XRaidLevel': {'$id': '7', '$type': 'Portal.Data.model.modelXRaidLevel, Portal.Data', 'ID': 6, 'DisplayText': 'Raid-5 (7D+1P)', 'XPool': [{'$ref': '3'}], 'XParityGroup': []}, 'Valid': {'$id': '8', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 1, 'DisplayText': 'Valid', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XPool1': []}, 'Normal': {'$id': '9', '$type': 'Portal.Data.model.modelNormal, Portal.Data', 'ID': 1, 'DisplayText': 'Warning', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XHdpTierVolume': []}, 'Valid1': {'$id': '10', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 2, 'DisplayText': 'Invalid', 'XPool': [], 'XPool1': [{'$ref': '1'}, {'$ref': '3'}]}}]}, 'Internal': {'$ref': '4'}, 'XEmulation': {'$ref': '5'}, 'PoolKind': {'$ref': '6'}, 'XRaidLevel': {'$id': '11', '$type': 'Portal.Data.model.modelXRaidLevel, Portal.Data', 'ID': 4, 'DisplayText': 'Raid-6 (14D+2P)', 'XPool': [{'$ref': '1'}], 'XParityGroup': []}, 'Valid': {'$ref': '8'}, 'Normal': {'$ref': '9'}, 'Valid1': {'$ref': '10'}}, {'$ref': '3'}]

My code:

response = request.urlopen(myurl)
json_response = response.read()
data = json.loads(json_response)
poolid = data[0]["PoolID"]
           for mypoolid in poolid:
              print("pool",mypoolid)

--> Other error occurred: 'int' object is not iterable

thanks for help.

Advertisement

Answer

If you need to get all objects from JSON with some specific key, you will need to traverse all elements. This way, for example:

import json

j = """
[{"$id": "1", "$type": "Portal.Data.model.modelXPool, Portal.Data", "SysId": 1234, "PoolID": 1, "PoolKindID": 1, "PoolRaidLevelMixableID": 1, "PoolEmulationTypeID": 1, "PoolSystemCapacityGb2": 637870.75, "PoolReservedCapacityGb2": 155320.92, "WarningThreshold": 70, "SystemThreshold": 80, "DepletionThreshold": 80, "ThresholdModeID": 2, "StatusValue": 1005, "StatusID": 1, "StatusDetail": "Utilization between 0% and Threshold", "NumOfPoolVolumes": 12, "MaxPagesOfCapacity": 124, "NumPagesUsedPool": 1234, "NumPagesForDpVol": 1234, "CapacityPerPageKb2": 43008, "MaxSubscription": 65535, "DriveInterfaceID": 1, "DriveSpeed": "", "DriveInternalExternalID": 1, "PoolRaidLevelID": 4, "NumOfTiers": 1, "PoolDriveInterface": {"$id": "2", "$type": "Portal.Data.model.modelPoolDriveInterface, Portal.Data", "ID": 1, "DisplayText": "SSD", "XPool": [{"$ref": "1"}, {"$id": "3", "$type": "Portal.Data.model.modelXPool, Portal.Data", "SysId": 1234, "PoolID": 90, "PoolKindID": 1, "PoolRaidLevelMixableID": 1, "PoolEmulationTypeID": 1, "PoolSystemCapacityGb2": 36858.77, "PoolReservedCapacityGb2": 11222.7, "WarningThreshold": 70, "SystemThreshold": 80, "DepletionThreshold": 80, "ThresholdModeID": 2, "StatusValue": 1005, "StatusID": 1, "StatusDetail": "Utilization between 0% and Threshold", "NumOfPoolVolumes": 12, "MaxPagesOfCapacity": 898652, "NumPagesUsedPool": 273620, "NumPagesForDpVol": 898588, "CapacityPerPageKb2": 43008, "MaxSubscription": 65535, "DriveInterfaceID": 1, "DriveSpeed": "", "DriveInternalExternalID": 1, "PoolRaidLevelID": 6, "NumOfTiers": 1, "PoolDriveInterface": {"$ref": "2"}, "Internal": {"$id": "4", "$type": "Portal.Data.model.modelInternal, Portal.Data", "ID": 1, "DisplayText": "Internal", "XPool": [{"$ref": "1"}, {"$ref": "3"}]}, "XEmulation": {"$id": "5", "$type": "Portal.Data.model.modelXEmulation, Portal.Data", "ID": 1, "DisplayText": "Open-V", "XPool": [{"$ref": "1"}, {"$ref": "3"}], "XParityGroup": []}, "PoolKind": {"$id": "6", "$type": "Portal.Data.model.modelPoolKind, Portal.Data", "ID": 1, "DisplayText": "DP", "XPool": [{"$ref": "1"}, {"$ref": "3"}]}, "XRaidLevel": {"$id": "7", "$type": "Portal.Data.model.modelXRaidLevel, Portal.Data", "ID": 6, "DisplayText": "Raid-5 (7D+1P)", "XPool": [{"$ref": "3"}], "XParityGroup": []}, "Valid": {"$id": "8", "$type": "Portal.Data.model.modelValid, Portal.Data", "ID": 1, "DisplayText": "Valid", "XPool": [{"$ref": "1"}, {"$ref": "3"}], "XPool1": []}, "Normal": {"$id": "9", "$type": "Portal.Data.model.modelNormal, Portal.Data", "ID": 1, "DisplayText": "Warning", "XPool": [{"$ref": "1"}, {"$ref": "3"}], "XHdpTierVolume": []}, "Valid1": {"$id": "10", "$type": "Portal.Data.model.modelValid, Portal.Data", "ID": 2, "DisplayText": "Invalid", "XPool": [], "XPool1": [{"$ref": "1"}, {"$ref": "3"}]}}]}, "Internal": {"$ref": "4"}, "XEmulation": {"$ref": "5"}, "PoolKind": {"$ref": "6"}, "XRaidLevel": {"$id": "11", "$type": "Portal.Data.model.modelXRaidLevel, Portal.Data", "ID": 4, "DisplayText": "Raid-6 (14D+2P)", "XPool": [{"$ref": "1"}], "XParityGroup": []}, "Valid": {"$ref": "8"}, "Normal": {"$ref": "9"}, "Valid1": {"$ref": "10"}}, {"$ref": "3"}]
"""

def get_all_elements_with_key(node, search_key):
    nodes = []
    if type(node) is dict:
        for k, v in node.items():
            new_entries = get_all_elements_with_key(v, search_key)
            if new_entries:
                nodes.extend(new_entries)
        if search_key in node:
            nodes.append(node)
    if type(node) is list:
        for el in node:
            new_entries = get_all_elements_with_key(el, search_key)
            if new_entries:
                nodes.extend(new_entries)
    return nodes if len(nodes) > 0 else None

data = json.loads(j)

result = get_all_elements_with_key(data, 'PoolID')
for node in result:
    print(node['PoolID'], node)

Output:

90 {'$id': '3', '$type': 'Portal.Data.model.modelXPool, Portal.Data', 'SysId': 1234, 'PoolID': 90, 'PoolKindID': 1, 'PoolRaidLevelMixableID': 1, 'PoolEmulationTypeID': 1, 'PoolSystemCapacityGb2': 36858.77, 'PoolReservedCapacityGb2': 11222.7, 'WarningThreshold': 70, 'SystemThreshold': 80, 'DepletionThreshold': 80, 'ThresholdModeID': 2, 'StatusValue': 1005, 'StatusID': 1, 'StatusDetail': 'Utilization between 0% and Threshold', 'NumOfPoolVolumes': 12, 'MaxPagesOfCapacity': 898652, 'NumPagesUsedPool': 273620, 'NumPagesForDpVol': 898588, 'CapacityPerPageKb2': 43008, 'MaxSubscription': 65535, 'DriveInterfaceID': 1, 'DriveSpeed': '', 'DriveInternalExternalID': 1, 'PoolRaidLevelID': 6, 'NumOfTiers': 1, 'PoolDriveInterface': {'$ref': '2'}, 'Internal': {'$id': '4', '$type': 'Portal.Data.model.modelInternal, Portal.Data', 'ID': 1, 'DisplayText': 'Internal', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XEmulation': {'$id': '5', '$type': 'Portal.Data.model.modelXEmulation, Portal.Data', 'ID': 1, 'DisplayText': 'Open-V', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XParityGroup': []}, 'PoolKind': {'$id': '6', '$type': 'Portal.Data.model.modelPoolKind, Portal.Data', 'ID': 1, 'DisplayText': 'DP', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XRaidLevel': {'$id': '7', '$type': 'Portal.Data.model.modelXRaidLevel, Portal.Data', 'ID': 6, 'DisplayText': 'Raid-5 (7D+1P)', 'XPool': [{'$ref': '3'}], 'XParityGroup': []}, 'Valid': {'$id': '8', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 1, 'DisplayText': 'Valid', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XPool1': []}, 'Normal': {'$id': '9', '$type': 'Portal.Data.model.modelNormal, Portal.Data', 'ID': 1, 'DisplayText': 'Warning', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XHdpTierVolume': []}, 'Valid1': {'$id': '10', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 2, 'DisplayText': 'Invalid', 'XPool': [], 'XPool1': [{'$ref': '1'}, {'$ref': '3'}]}}
1 {'$id': '1', '$type': 'Portal.Data.model.modelXPool, Portal.Data', 'SysId': 1234, 'PoolID': 1, 'PoolKindID': 1, 'PoolRaidLevelMixableID': 1, 'PoolEmulationTypeID': 1, 'PoolSystemCapacityGb2': 637870.75, 'PoolReservedCapacityGb2': 155320.92, 'WarningThreshold': 70, 'SystemThreshold': 80, 'DepletionThreshold': 80, 'ThresholdModeID': 2, 'StatusValue': 1005, 'StatusID': 1, 'StatusDetail': 'Utilization between 0% and Threshold', 'NumOfPoolVolumes': 12, 'MaxPagesOfCapacity': 124, 'NumPagesUsedPool': 1234, 'NumPagesForDpVol': 1234, 'CapacityPerPageKb2': 43008, 'MaxSubscription': 65535, 'DriveInterfaceID': 1, 'DriveSpeed': '', 'DriveInternalExternalID': 1, 'PoolRaidLevelID': 4, 'NumOfTiers': 1, 'PoolDriveInterface': {'$id': '2', '$type': 'Portal.Data.model.modelPoolDriveInterface, Portal.Data', 'ID': 1, 'DisplayText': 'SSD', 'XPool': [{'$ref': '1'}, {'$id': '3', '$type': 'Portal.Data.model.modelXPool, Portal.Data', 'SysId': 1234, 'PoolID': 90, 'PoolKindID': 1, 'PoolRaidLevelMixableID': 1, 'PoolEmulationTypeID': 1, 'PoolSystemCapacityGb2': 36858.77, 'PoolReservedCapacityGb2': 11222.7, 'WarningThreshold': 70, 'SystemThreshold': 80, 'DepletionThreshold': 80, 'ThresholdModeID': 2, 'StatusValue': 1005, 'StatusID': 1, 'StatusDetail': 'Utilization between 0% and Threshold', 'NumOfPoolVolumes': 12, 'MaxPagesOfCapacity': 898652, 'NumPagesUsedPool': 273620, 'NumPagesForDpVol': 898588, 'CapacityPerPageKb2': 43008, 'MaxSubscription': 65535, 'DriveInterfaceID': 1, 'DriveSpeed': '', 'DriveInternalExternalID': 1, 'PoolRaidLevelID': 6, 'NumOfTiers': 1, 'PoolDriveInterface': {'$ref': '2'}, 'Internal': {'$id': '4', '$type': 'Portal.Data.model.modelInternal, Portal.Data', 'ID': 1, 'DisplayText': 'Internal', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XEmulation': {'$id': '5', '$type': 'Portal.Data.model.modelXEmulation, Portal.Data', 'ID': 1, 'DisplayText': 'Open-V', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XParityGroup': []}, 'PoolKind': {'$id': '6', '$type': 'Portal.Data.model.modelPoolKind, Portal.Data', 'ID': 1, 'DisplayText': 'DP', 'XPool': [{'$ref': '1'}, {'$ref': '3'}]}, 'XRaidLevel': {'$id': '7', '$type': 'Portal.Data.model.modelXRaidLevel, Portal.Data', 'ID': 6, 'DisplayText': 'Raid-5 (7D+1P)', 'XPool': [{'$ref': '3'}], 'XParityGroup': []}, 'Valid': {'$id': '8', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 1, 'DisplayText': 'Valid', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XPool1': []}, 'Normal': {'$id': '9', '$type': 'Portal.Data.model.modelNormal, Portal.Data', 'ID': 1, 'DisplayText': 'Warning', 'XPool': [{'$ref': '1'}, {'$ref': '3'}], 'XHdpTierVolume': []}, 'Valid1': {'$id': '10', '$type': 'Portal.Data.model.modelValid, Portal.Data', 'ID': 2, 'DisplayText': 'Invalid', 'XPool': [], 'XPool1': [{'$ref': '1'}, {'$ref': '3'}]}}]}, 'Internal': {'$ref': '4'}, 'XEmulation': {'$ref': '5'}, 'PoolKind': {'$ref': '6'}, 'XRaidLevel': {'$id': '11', '$type': 'Portal.Data.model.modelXRaidLevel, Portal.Data', 'ID': 4, 'DisplayText': 'Raid-6 (14D+2P)', 'XPool': [{'$ref': '1'}], 'XParityGroup': []}, 'Valid': {'$ref': '8'}, 'Normal': {'$ref': '9'}, 'Valid1': {'$ref': '10'}}
Advertisement