# Weird JSONata problem

**URL:** <https://discourse.nodered.org/t/weird-jsonata-problem/2408>\
**Category:** General\
**Created:** [19 August 2018 21:11 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408 "2018-08-19T21:11:43Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [19 August 2018 21:11 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/1 "2018-08-19T21:11:43Z")

</div>

I have just cloned a repository to version 0.19.1 on a new installation of Raspian Lite on a Raspberry Pi of a project that was working on 0,18.4.

My original Change node JSONata expression was:  
 ![Screenshot%20at%202018-08-19%2021-55-24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6af04261a3121910c84dcd599b7c6dee6439d9a9.png)  
[EDIT] (payload.1 + 256 \* payload.0)) / 1.2

Which now throws this error:  
 ![Screenshot%20at%202018-08-19%2021-55-46](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/26c75502dc44b72b1e64a0a8c70c52970305bef6.png)

But this works:

 ![Screenshot%20at%202018-08-19%2021-53-29](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/88240e1880eb79b6fde5e94f6e3b1a789555cee4.png)

Any ideas why the original JSONata is not working?

---

<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:** [19 August 2018 21:16 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/2 "2018-08-19T21:16:56Z")

</div>

I should have said that the payload is a 2 byte buffer!

---

<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:** [19 August 2018 21:35 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/3 "2018-08-19T21:35:40Z")

</div>

mm - While trying to see if I could replicate it (I can) I decided to see what happens if I just use change node to extract payload[1] (I'm injecting a 2 byte buffer) and use debug node in complete object mode

I just didn't get a payload???

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/15f70187338be65de6ba85f0febbde2265cc392e.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:** [19 August 2018 21:37 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/4 "2018-08-19T21:37:38Z")

</div>

Without the change node to prove I'm sending a 2 byte buffer

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e435cde00bd18e2faeb1fa17026750c9ec86d53f.png)

---

<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:** [19 August 2018 21:40 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/5 "2018-08-19T21:40:51Z")

</div>

Yes, if I route the output of the node sending the buffer (an I2C node reading a sensor) to a debug node I get the same sort of result as you, I'm glad you could reproduce the behaviour.

---

<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:** [19 August 2018 21:43 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/6 "2018-08-19T21:43:46Z")

</div>

I'm wondering if its actually valid to use payload[x] to access a byte within a buffer object ?

I googled and ended up finding my own old thread in google groups 🙂  
[https://groups.google.com/forum/#!topic/node-red/XGjHudM2oPA](https://groups.google.com/forum/#!topic/node-red/XGjHudM2oPA)

---

<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:** [19 August 2018 21:49 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/7 "2018-08-19T21:49:06Z")

</div>

Well outside of JSONata payload[x] does work.

---

<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:** [19 August 2018 22:13 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/8 "2018-08-19T22:13:06Z")

</div>

> [@ghayne](#):
>
> Well outside of JSONata payload does work.

Yes - because its pure NodeRED valid syntax - so it must be right 🙂

But using "payload" in JSONata must be a down to a modification to JSONata as used in NodeRED (as the basic JSONata won't know anything about NodeRED syntax I'd thought)

---

<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:** [19 August 2018 22:17 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/9 "2018-08-19T22:17:17Z")

</div>

Actually my original syntax in the JSONata expression was:  
payload.1 and payload.0 not payload[1] and payload[0].  
which worked.

---

<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:** [19 August 2018 22:23 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/10 "2018-08-19T22:23:08Z")

</div>

aah 🙂

So I'm thinking that either you were lucky when NodeRED was using the old syntax and unlucky now or that a bug has been introduced

JFI your workaround flow doesn't work for me

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/03303032ec6552aaf248be5a3924fbc9257e5e2a.png)

---

<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:** [19 August 2018 22:27 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/11 "2018-08-19T22:27:12Z")

</div>

Simon,  
I assigned the values of "one" and "zero" with msg.payload[1] and msg.payload[0], not with a JSONata expression!

---

<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:** [19 August 2018 22:34 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/12 "2018-08-19T22:34:52Z")

</div>

Oops - so you did 😮

It works for me as well 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/960e7476451b3474d35d96d2e33232d42aa9f0fc.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:** [19 August 2018 22:36 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/13 "2018-08-19T22:36:56Z")

</div>

I think lets call it a bug then 🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 August 2018 23:19 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/14 "2018-08-19T23:19:18Z")

</div>

Can we have a succinct summary of what you believe the bug to be, along with an example flow?

---

<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:** [20 August 2018 09:09 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/15 "2018-08-20T09:09:44Z")

</div>

@knolleary

In a nutshell:

```auto
[{"id":"83c2ccdb.1ad86","type":"inject","z":"a7b4738a.2642c","name":"","topic":"","payload":"[50,20]","payloadType":"bin","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":180,"wires":[["760ac86e.44acc8"]]},{"id":"55fd7cfb.0fb8a4","type":"debug","z":"a7b4738a.2642c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":510,"y":180,"wires":[]},{"id":"760ac86e.44acc8","type":"change","z":"a7b4738a.2642c","name":"","rules":[{"t":"set","p":"works","pt":"msg","to":"payload[1]","tot":"msg"},{"t":"set","p":"doesnt","pt":"msg","to":"payload[1]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":180,"wires":[["55fd7cfb.0fb8a4"]]}]

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [20 August 2018 09:17 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/16 "2018-08-20T09:17:10Z")

</div>

Thanks for that.

Unfortunately, this is a limitation of JSONata.

JSONata is intended for use with JSON data. Buffer is not part of the JSON standard so isn't natively supported for those Array-like operations.

---

<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:** [20 August 2018 09:33 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/17 "2018-08-20T09:33:01Z")

</div>

Cheers Nick,  
we discussed this some months ago in the old forum, it's just funny that my original flow stopped working after moving from 0.18.4 to 0.19.1.

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [20 August 2018 17:42 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/18 "2018-08-20T17:42:57Z")

</div>

Might this be what you are looking for?

**Note** the changes to the injection node to JSON an the change node to JSONata.

```auto
[{"id":"923ab90b.e57b88","type":"inject","z":"e6a05159.0272d","name":"","topic":"","payload":"[50,20]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":161.01421356201172,"y":144.0056972503662,"wires":[["9b4c67c9.765b58"]]},{"id":"9077b14.13f9f5","type":"debug","z":"e6a05159.0272d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":735.0142211914062,"y":144.0056915283203,"wires":[]},{"id":"9b4c67c9.765b58","type":"change","z":"e6a05159.0272d","name":"","rules":[{"t":"set","p":"zero","pt":"msg","to":"$.payload[0]","tot":"jsonata"},{"t":"set","p":"one","pt":"msg","to":"$.payload[1]","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"($.payload[1] + (256*$.payload[0]))/1.2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":525.0142211914062,"y":144.0056915283203,"wires":[["9077b14.13f9f5"]]}]

```

This returns the same result in 0.18 and 0.19.

```auto
  {
     "_msgid":"d1582f7d.0522c",
     "topic":"",
     "payload":10683.333333333334,
     "zero":50,
     "one":20
  }

```

---

<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:** [20 August 2018 18:17 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/19 "2018-08-20T18:17:34Z")

</div>

> [@ncherry](#):
>
> [{"id":"923ab90b.e57b88","type":"inject","z":"e6a05159.0272d","name":"","topic":"","payload":"[50,20]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":161.01421356201172,"y":144.0056972503662,"wires":[["9b4c67c9.765b58"]]},{"id":"9077b14.13f9f5","type":"debug","z":"e6a05159.0272d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":735.0142211914062,"y":144.0056915283203,"wires":},{"id":"9b4c67c9.765b58","type":"change","z":"e6a05159.0272d","name":"","rules":[{"t":"set","p":"zero","pt":"msg","to":"$.payload[0]","tot":"jsonata"},{"t":"set","p":"one","pt":"msg","to":"$.payload[1]","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"($.payload[1] + (256\*$.payload[0]))/1.2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":525.0142211914062,"y":144.0056915283203,"wires":[["9077b14.13f9f5"]]}]

Unfortunately not as the OP's source is a buffer object not a "normal" object

Change the source to a buffer and it fails ☹

---

<div class="post-metadata">

**Author:** ![ncherry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ncherry/32/27_2.png) [@ncherry](https://discourse.nodered.org/u/ncherry)\
**Post date:** [20 August 2018 18:22 UTC](https://discourse.nodered.org/t/weird-jsonata-problem/2408/20 "2018-08-20T18:22:30Z")

</div>

Thanks, that does help me in that I didn't know there was a buffer object. I'll need to adjust some of my code to do a node.error on that. And I'll go look up a buffer object so I understand that better.

[Next page](https://discourse.nodered.org/t/weird-jsonata-problem/2408.md?page=2)
