script transformation

Changes Message payload, Metadata or Message type using configured JavaScript function.

JavaScript function receives 3 input parameters:

  • msg - is a Message payload.

  • metadata - is a Message metadata.

  • msgType - is a Message type.

Script should return the following structure:

{   
    msg: new payload,
    metadata: new metadata,
    msgType: new msgType 
}

All fields in resulting object are optional and will be taken from original message if not specified.

Outbound Message from this Node will be new Message that was constructed using configured JavaScript function.

JavaScript transform function can be verified using Test JavaScript function.

Example

Node receives Message with payload:

{
    "temperature": 22.4,
    "humidity": 78
}

Original Metadata:

{ "sensorType" : "temperature" }

Original Message Type - POST_TELEMETRY_REQUEST

The following modifications should be performed:

  • change message type to ‘CUSTOM_UPDATE’

  • add additional attribute version into payload with value v1.1

  • change sensorType attribute value in Metadata to roomTemp

The following transform function will perform all necessary modifications:

var newType = "CUSTOM_UPDATE";
msg.version = "v1.1";
metadata.sensorType = "roomTemp"
return {msg: msg, metadata: metadata, msgType: newType};

Last updated