How to use Function to select byte array and display string

I'm new to this, and have been playing around, but to no avail.
I have the following object being delivered from an mqtt broker.

{
    "uplink_message": {
        "session_key_id": "AX88sOZsNTkoN5rZ+Rzx/Q==",
        "f_port": 1,
        "f_cnt": 80,
        "frm_payload": "SGVsbG8sIHdvcmxkIQ==",
        "decoded_payload": {
            "bytes": [
                72,
                101,
                108,
                108,
                111,
                44,
                32,
                119,
                111,
                114,
                108,
                100,
                33
            ]
        }
    }
}

Now I'm trying to pull out the array by itself in a function and display the string which is simply "Hello World!"

I can't quite figure out the function to select just the array and display the string. Maybe there is another way rather than using a function node. Any help would be appreciated.

Try Buffer.from(xxx).toString() where xxx is the full path to the bytes array.

I think the "full path" is what I'm having trouble with. I've tried variations of "uplink_message.decoded_payload.bytes" but no go.

I think I figured it out. I converted to string in MQTT payload formatter with:

function decodeUplink(input) {
    var msg = "";
    var data = {};
for (var i = 0; i < input.bytes.length; i++) {
        msg += (String.fromCharCode(input.bytes[i]));
    }
    data.message = msg;
    return {
        data: data,
     }
}

Then in the function node:

var thing = [{
	session_key_id: msg.payload.uplink_message.session_key_id,
	f_port: msg.payload.uplink_message.f_port,
	f_cnt: msg.payload.uplink_message.f_cnt,
	frm_payload: msg.payload.uplink_message.frm_payload,
}]
msg.payload = thing;
return msg;

In a function node, you always access properties via the msg object.

E.g. msg.payload or msg.topic etc.

Canned text...

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.