0

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.

2
  • Getaway is a a list of dict, so you can retrieve the list and iterate on it. Commented Aug 12, 2021 at 9:51
  • for gw in event['WirelessMetadata']['WirelessMetadata']['Gateways']: do something with gw Commented Aug 12, 2021 at 9:54

1 Answer 1

1

You can iterate through the list of gateways.

gateways = event['WirelessMetadata']['WirelessMetadata']['Gateways']

amount_of_gateways = len(gateways)

for gateway in gateways:
    print(gateway["Rssi"])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.