I have the below JSON output ... I need to read this JSON dictionary and write the 3 'url' values in BOLD to a variable...
data = json.loads(wm_data)
varlayers = data['operationalLayers']['layers']['url']
Not sure how to and how to loop through the entire JSON to capture all 3... and it might be only 2 sometimes and maybe more... Any ideas.
{'operationalLayers': [{'id': '19397948adc-layer-1', 'title': 'Neighborhood Utility Permits uat', 'layers': [{'id': '19397948c74-layer-2', 'title': 'Neighborhood Permit Polygon', 'url': 'https://services.arcgis.com/pxxxxxxxxxxl7/arcgis/rest/services/Neighborhood_Utility_Permits_uat/Feat...', 'itemId': '63721bd01f5a475291066c688c7feab8', 'layerType': 'ArcGISFeatureLayer', 'popupInfo': {'popupElements': [{'type': 'fields', 'description': '', 'fieldInfos': [{'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': ''}, {'type': 'attachments', 'description': '', 'displayType': 'preview', 'title': ''}], 'showAttachments': True, 'expressionInfos': [], 'fieldInfos': [{'fieldName': 'OBJECTID', 'format': {'digitSeparator': False, 'places': 0}, 'isEditable': False, 'label': 'OBJECTID', 'visible': False}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'GLOBALID', 'isEditable': False, 'label': 'GLOBALID', 'visible': False}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': 'Neighborhood Utility Permit Polygon'}}, {'id': '19397948c75-layer-3', 'title': 'Neighborhood Permit Line', 'url': 'https://services.arcgis.com/pxxxxxxxxxxxx/arcgis/rest/services/Neighborhood_Utility_Permits_uat/Feat...', 'itemId': '63721bd01f5a475291066c688c7feab8', 'layerType': 'ArcGISFeatureLayer', 'popupInfo': {'popupElements': [{'type': 'fields', 'description': '', 'fieldInfos': [{'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': ''}, {'type': 'attachments', 'description': '', 'displayType': 'preview', 'title': ''}], 'showAttachments': True, 'expressionInfos': [], 'fieldInfos': [{'fieldName': 'OBJECTID', 'format': {'digitSeparator': False, 'places': 0}, 'isEditable': False, 'label': 'OBJECTID', 'visible': False}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'GLOBALID', 'isEditable': False, 'label': 'GLOBALID', 'visible': False}, {'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': 'Neighborhood Utility Permit Line'}}, {'id': '19397948c75-layer-4', 'title': 'Neighborhood Permit Point', 'url': 'https://services.arcgis.com/p5xxxxxxxxxx7/arcgis/rest/services/Neighborhood_Utility_Permits_uat/Feat...', 'itemId': '63721bd01f5a475291066c688c7feab8', 'layerType': 'ArcGISFeatureLayer', 'popupInfo': {'popupElements': [{'type': 'fields', 'description': '', 'fieldInfos': [{'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': ''}, {'type': 'attachments', 'description': '', 'displayType': 'preview', 'title': ''}], 'showAttachments': True, 'expressionInfos': [], 'fieldInfos': [{'fieldName': 'OBJECTID', 'format': {'digitSeparator': False, 'places': 0}, 'isEditable': False, 'label': 'OBJECTID', 'visible': False}, {'fieldName': 'CUSTOMER_NAME', 'isEditable': True, 'label': 'Customer Name', 'visible': True}, {'fieldName': 'PERMIT_NUMBER', 'isEditable': True, 'label': 'Permit Number', 'visible': True}, {'fieldName': 'GLOBALID', 'isEditable': False, 'label': 'GLOBALID', 'visible': False}, {'fieldName': 'ANTICIPATEDSTART', 'isEditable': True, 'label': 'Anticipated Start Date', 'visible': True}, {'fieldName': 'ANTICIPATEDCOMPLETE', 'isEditable': True, 'label': 'Anticipated Completed Date', 'visible': True}], 'title': 'Neighborhood Utility Permit Point'}}], 'layerType': 'GroupLayer'}], 'baseMap': {'baseMapLayers': [{'id': 'VectorTile_3714', 'opacity': 1, 'title': 'Human Geography Base', 'visibility': True, 'layerType': 'VectorTileLayer', 'styleUrl': 'https://cdn.arcgis.com/sharing/rest/content/items/xyzxyzxyz/resources/styles/root.json'}, {'id': 'VectorTile_9220', 'opacity': 0.38, 'title': 'Human Geography Detail', 'visibility': True, 'layerType': 'VectorTileLayer', 'styleUrl': 'https://cdn.arcgis.com/sharing/rest/content/items/xyzxyzxyzxy/resources/styles/root.json', 'isReference': True}, {'id': 'VectorTile_1103', 'opacity': 1, 'title': 'Human Geography Label', 'visibility': True, 'layerType': 'VectorTileLayer', 'styleUrl': 'https://cdn.arcgis.com/sharing/rest/content/items/xyzxyzxyzxy/resources/styles/root.json', 'isReference': True}], 'title': 'Human Geography Map'}, 'authoringApp': 'ArcGISMapViewer', 'authoringAppVersion': '2024.2', 'initialState': {'viewpoint': {'targetGeometry': {'spatialReference': {'latestWkid': 3857, 'wkid': 102100}, 'xmin': -9361030.36503893, 'ymin': 4281372.106805783, 'xmax': -8353896.080353716, 'ymax': 4839056.665174281}}}, 'spatialReference': {'latestWkid': 3857, 'wkid': 102100}, 'timeZone': 'system', 'version': '2.32'}
The reason your current code is failing is that operationalLayers is a list (indicated by the [ bracket), and inside that list, your layers are nested inside a Group Layer.
To get all those URLs regardless of how many there are, you need to loop through the operational layers and then loop through the sub-layers inside them.
Here is a simple way to do it using a list comprehension:
# Create a list to store all URLs found
urls = []
# Loop through the operational layers
for op_layer in data.get('operationalLayers', []):
# Check if this is a group layer containing a sub-list of layers
if 'layers' in op_layer:
for sub_layer in op_layer['layers']:
if 'url' in sub_layer:
urls.append(sub_layer['url'])
# Also check the top-level op_layer in case it's not a group
elif 'url' in op_layer:
urls.append(op_layer['url'])
print(urls)