I'm trying to work with the following incoming json in a Python script running in an AWS Lambda function. This is coming into Lambda on the usual event object as a dict.
{
"WirelessDeviceId": "AAAAA-BBBBB-CCCC-DDDD-EEEEEEEEEEE",
"AddedDeviceDecoderType": "TEST",
"PayloadData": "Aw==",
"WirelessMetadata": {
"LoRaWAN": {
"ADR": true,
"Bandwidth": 125,
"ClassB": false,
"CodeRate": "4/5",
"DataRate": "3",
"DevAddr": "LMNOP123",
"DevEui": "DPLAMSD010101",
"FCnt": 42,
"FOptLen": 1,
"FPort": 4,
"Frequency": "905100000",
"Gateways": [
{
"GatewayEui": "ABC123",
"Rssi": -109,
"Snr": 8
}
],
"MIC": "ABC900000",
"MType": "UnconfirmedDataUp",
"Major": "LoRaWANR1",
"Modulation": "LORA",
"PolarizationInversion": false,
"SpreadingFactor": 7,
"Timestamp": "2021-08-08T16:04:10Z"
}
}
}
I'm struggling with the "Gateways" array, which may have anywhere from 1 to 10 entries.
Right now, during testing I only have one gateway, and am addressing it in Python like this.
rssi = event['WirelessMetadata']['WirelessMetadata']['Gateways'][0]['Rssi']
But when new gateways are added, I won't know how many Gateways will be inside that array, nor will I know what index they are assigned to so very quickly hard-coding the [0] will not work.
Newbie question, but what is the best way to handle that case? I thought it might have something to do with looping through the array, but I struggled a bit to understand best practice.
for gw in event['WirelessMetadata']['WirelessMetadata']['Gateways']: do something with gw