# Can I use JSONata to access elements of a msg.payload when it's a buffer of byte?

**URL:** <https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250>\
**Category:** General\
**Created:** [22 October 2023 09:51 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250 "2023-10-22T09:51:25Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 09:51 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/1 "2023-10-22T09:51:25Z")

</div>

Google is getting me nowhere ☹

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [22 October 2023 10:03 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/2 "2023-10-22T10:03:15Z")

</div>

Can't you use logic operators?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 10:05 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/3 "2023-10-22T10:05:27Z")

</div>

I don't see any logic operators ☹

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [22 October 2023 10:11 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/4 "2023-10-22T10:11:14Z")

</div>

Have a look here:

> <https://stackoverflow.com/questions/74849922/jsonata-bit-manipulation>

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 10:24 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/5 "2023-10-22T10:24:29Z")

</div>

That needs JSONata to be extended - which I'm sure is way past my paygrade 🙂

Plus. I'm after access to buffer values e.g. payload.0 payload.1 etc

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 10:28 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/6 "2023-10-22T10:28:05Z")

</div>

my actual need is to use JSONata to multiply payload.4 by 256 and then add it to payload.5 to get a 16bit length value

I wonder if the buffer-parser node would let me do this without need for JSONAta?

But I've never managed to understand how to use it ☹

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf7640c4bea3ab6ed8b630ce7cbe6c6a1c023e87.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 October 2023 10:48 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/7 "2023-10-22T10:48:24Z")

</div>

To some extent yes

```auto
(
    $data := $$.payload.*;
    $data[4] * 256 + $data[5]
)

```

```auto
[{"id":"783f92be64ac750f","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,2,3,4,5,6]","payloadType":"bin","x":90,"y":4840,"wires":[["a442df240b88c426"]]},{"id":"a442df240b88c426","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $data := $$.payload.*;\t $data[4] * 256 + $data[5]\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":4840,"wires":[["7f97c85a28ce873c"]]},{"id":"7f97c85a28ce873c","type":"debug","z":"b779de97.b1b46","name":"debug 347","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":4840,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 10:56 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/8 "2023-10-22T10:56:48Z")

</div>

That worked 🙂  
Why do you say to some extent?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 October 2023 10:59 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/9 "2023-10-22T10:59:43Z")

</div>

Because I was not sure what your actual buffer was, and as others have said if you want to preform bitwise operations I think you you would need to extend JSONata.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 11:02 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/10 "2023-10-22T11:02:12Z")

</div>

> [@E1cid](#):
>
> and as others have said if you want to preform bitwise operations

ghayne just misunderstood what I was after 🙂

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [22 October 2023 11:03 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/11 "2023-10-22T11:03:19Z")

</div>

You can not blame him, I was unsure until you clarified his responses.

---

<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:** [22 October 2023 11:28 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/12 "2023-10-22T11:28:56Z")

</div>

> [@cymplecy](#):
>
> But I've never managed to understand how to use it

It's not so bad once you "get it". And once you do, you realise why it's laid out as it is and it's power.

You can of course read the explanations in the built in help but [this example](https://discourse.nodered.org/t/scaled-value-while-reading-modbus/75871/4) should give you a head start:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6d70713461c4d7c154e73efabe462fb28784beaf.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 11:39 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/13 "2023-10-22T11:39:47Z")

</div>

That makes sense 🙂  
This seems to do the job for me to produce topicLength

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4f66feda44cbc249ef4baeaf246abdace89bacf5.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 16:21 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/14 "2023-10-22T16:21:16Z")

</div>

Follow up question

Can I make msg.payload be the output rather than a sub property of msg.payload.

Or do I have to use a change node on the output?

I tried leaving Output property empty but that didn't do the job for me

---

<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:** [22 October 2023 16:57 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/15 "2023-10-22T16:57:05Z")

</div>

By default, it works in object mode (putting everything in payload). If you chose fan out, each value you ask for will go out of its own pin. If there is only one value to get" there will be only one output. What it outputs on that single pin depends on what you chose for the final "output" drop-down (can't remember options & not at computer - I think there is an entry named "value")

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [22 October 2023 17:02 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/16 "2023-10-22T17:02:43Z")

</div>

Does the job - thanks very much

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b58cdc32aa68f0a417fa3cdb6605107996dc521d.png)

---

<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:** [21 December 2023 17:03 UTC](https://discourse.nodered.org/t/can-i-use-jsonata-to-access-elements-of-a-msg-payload-when-its-a-buffer-of-byte/82250/17 "2023-12-21T17:03:13Z")

</div>

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