Weird JSONata problem

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
[EDIT] (payload.1 + 256 * payload.0)) / 1.2

Which now throws this error:
Screenshot%20at%202018-08-19%2021-55-46

But this works:

Any ideas why the original JSONata is not working?

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

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

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

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.

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 :slight_smile:
https://groups.google.com/forum/#!topic/node-red/XGjHudM2oPA

1 Like

Well outside of JSONata payload[x] does work.

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

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)

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

aah :slight_smile:

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

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

1 Like

Oops - so you did :open_mouth:

It works for me as well :slight_smile:

I think lets call it a bug then :slight_smile:

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

@knolleary

In a nutshell:

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

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.

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.

Might this be what you are looking for?

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

[{"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.

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

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

Change the source to a buffer and it fails :frowning:

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.