# How to use Function to select byte array and display string

**URL:** <https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085>\
**Category:** General\
**Tags:** function-node\
**Created:** [27 February 2022 22:18 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085 "2022-02-27T22:18:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)\
**Post date:** [27 February 2022 22:18 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/1 "2022-02-27T22:18:49Z")

</div>

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.

```auto
{
    "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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 February 2022 22:23 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/2 "2022-02-27T22:23:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)\
**Post date:** [27 February 2022 22:30 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/3 "2022-02-27T22:30:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)\
**Post date:** [27 February 2022 23:36 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/4 "2022-02-27T23:36:08Z")

</div>

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

```auto
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:

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 February 2022 23:46 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/5 "2022-02-27T23:46:28Z")

</div>

> [@rfrancis97](#):
>
> I think the "full path" is what I'm having trouble with. I've tried variations of "uplink\_message.decoded\_payload.bytes

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](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![rfrancis97](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rfrancis97](https://discourse.nodered.org/u/rfrancis97)\
**Post date:** [28 February 2022 00:12 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/6 "2022-02-28T00:12:22Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 March 2022 00:13 UTC](https://discourse.nodered.org/t/how-to-use-function-to-select-byte-array-and-display-string/59085/7 "2022-03-14T00:13:11Z")

</div>

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