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": []
    }
]
TotallyInformation

Flow Info

created 2 weeks, 5 days ago

Node Types

Core
  • change (x1)
  • debug (x1)
Other
  • moment (x1)

Tags

  • JSONata
  • Data-Restructuring
  • Data-Manipulation
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option