Pre-parser

Use the pre-parser to format your payload, split it into multiple payloads and to edit its timestamp.


Payload formatting

Before we send your payload to the Device Definition, you can format your payload using the Pre-parser. This can be useful when your payload has a data structure that we do not recognize, or if your payload uses a specific identifier for device names.

The pre-parser always needs to return at leasts two things; a deviceName and a payload.

Pre-parser example

const parsePayload = (payloadStr) => {
  const json = JSON.parse(payloadStr)
  let deviceName, payload

  // Example deviceName
  if (json.devEUI) {
    deviceName = json.devEUI
  }

  // Example payload
  if (json.data) {
    payload = json.data
  }

  return {
    deviceName,
    payload,
  }
}

Timestamps

By default, our gateway automatically assigns a timestamp to the payload when it is received. However, you can configure the pre-parser to use a custom timestamp for your payload if needed, overriding the default behavior.

Pre-parser using custom timestamps

const parsePayload = (payloadStr) => {
  const json = JSON.parse(payloadStr)
  let deviceName, payload, time

  // Example timestamp
  if (json.timestampInMs) {
    time = json.timestampInMs
  }

  return {
    deviceName,
    payload,
    time,
  }
}

Splitting payloads

It is possible for the pre-parser to take one large payload and split it into different, smaller payloads. This is very useful when working with devices that periodically send one payload that contains measurements from a longer time period.

For this example, we will be using the following JSON payload:

Example JSON payload

{
  "devEUI": "myDeviceName",
  "measurements": [
    {
      "temperature": 22.5,
      "humidity": 45.2,
      "timestampInMs": 1727085443874
    },
    {
      "temperature": 22.7,
      "humidity": 45.1,
      "timestampInMs": 1727085518934
    },
    {
      "temperature": 21.9,
      "humidity": 44.6,
      "timestampInMs": 1727085620422
    }
  ]
}

This payload can be split into multiple payloads by returning a new object with a deviceName, payload and timestamp per measurement.

Pre-parser splitting data into multiple payloads

const parsePayload = (payloadStr) => {
  const json = JSON.parse(payloadStr)
  let deviceName, payload

  ...

  if (json.measurements?.length > 0) {
    return json.measurements.map((measurement) => ({
      deviceName,
      payload: measurement,
      time: measurement.timestampInMs,
    }))
  }

  // this return statement is not used in this example, but is normally used by other payloads.
  return {
    deviceName,
    payload,
  }
}

As a result, the payload per measurement would look like this:

Example output

{
  "temperature": 22.5,
  "humidity": 45.2,
  "timestampInMs": 1727085443874
}