Skip to content

Instantly share code, notes, and snippets.

@TotallyInformation
Last active September 7, 2019 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TotallyInformation/470b3be54e42619e801ab1e36ffc854a to your computer and use it in GitHub Desktop.
Save TotallyInformation/470b3be54e42619e801ab1e36ffc854a to your computer and use it in GitHub Desktop.
JSONata Example: Changing topic and payload

OK, so I've been a bit slow in getting my head around JSONata. I knew it was going to be valuable but I've just had my first actual use case.

I needed to rebuild an output payload from several parts of the input so I could monitor inputs from an RFXtrx433E. I wired up all the input nodes from Max's RFX node, added a timestamp using my moment node then added a change node with 2 changes.

The first change alters the msg.topic - I wanted to add RFX/ to the beginning to push the MQTT output messages to a new, dedicated path. Here is the JSONata for that:

"RFX/" & topic

The second change pushes the msg down onto the msg.payload. Unfortunately, although you can grab the whole msg object using "$" in JSONata, Node-RED will not let you put the msg object into the payload directly as it thinks you are creating a circular object definition. So instead, we build the payload manually:

{ 
  "topic": topic, 
  "status": status,
  "payload": payload, 
  "timestamp": timestamp,
  "_msgid": _msgid
}

I haven't included the RFX input nodes in the following flow as they won't work anyway unless you have the right hardware but hopefully you can see the idea. I've also missed off the MQTT output node as it can be annoying to end up with other people's MQTT configuration nodes.

I can very much recommend this talk from Andrew Coleman at IBM if you want to learn the basics of JSONata.

[
{
"id": "a04d87ad.308bd8",
"type": "moment",
"z": "df20f5b7.b6f788",
"name": "Insert Timestamp",
"topic": "",
"input": "",
"inputType": "date",
"inTz": "Europe/London",
"adjAmount": 0,
"adjType": "days",
"adjDir": "add",
"format": "",
"locale": "en_GB",
"output": "timestamp",
"outputType": "msg",
"outTz": "Europe/London",
"x": 470,
"y": 740,
"wires": [
[
"98d1b965.3d1628"
]
]
},
{
"id": "98d1b965.3d1628",
"type": "change",
"z": "df20f5b7.b6f788",
"name": "",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "{ \t \"topic\": topic, \t \"status\": status,\t \"payload\": payload, \t \"timestamp\": timestamp,\t \"_msgid\": _msgid\t } ",
"tot": "jsonata"
},
{
"t": "set",
"p": "topic",
"pt": "msg",
"to": "\"RFX/\" & topic",
"tot": "jsonata"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 680,
"y": 740,
"wires": [
[
"75a275dd.844f0c",
"14c8f280.fc73bd"
]
]
},
{
"id": "75a275dd.844f0c",
"type": "debug",
"z": "df20f5b7.b6f788",
"name": "",
"active": true,
"console": "true",
"complete": "true",
"x": 830,
"y": 700,
"wires": []
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment