# JSONata examples flows.node-red.org not working

**URL:** <https://discourse.nodered.org/t/jsonata-examples-flows-node-red-org-not-working/86179>\
**Category:** General\
**Created:** [6 March 2024 06:12 UTC](https://discourse.nodered.org/t/jsonata-examples-flows-node-red-org-not-working/86179 "2024-03-06T06:12:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![christopher-lambe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christopher-lambe/32/69369_2.png) [@christopher-lambe](https://discourse.nodered.org/u/christopher-lambe)\
**Post date:** [6 March 2024 06:12 UTC](https://discourse.nodered.org/t/jsonata-examples-flows-node-red-org-not-working/86179/1 "2024-03-06T06:12:15Z")

</div>

Hi,

I am tring to improve my JSONata skills (Yup, I'm a SQL guy loving JSONata aggregate functions...) So I downloaded the flows from [https://flows.nodered.org/flow/29fd01f8a62fec86d875ecbd68001cb0](https://flows.nodered.org/flow/29fd01f8a62fec86d875ecbd68001cb0) and tried them out. All the exmples return arrays of empty objects. I suspect the evaluate method of JSONata is asynchronous and that is the problem here. But promises and callbacks and awaits and node.send hurt my brain and I give up. More coffee is not helping.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a85b2071b073df2b8d4cf48dce85207789da79ca.png)

Can anybody please show me how to get this simple example working?

Here is a simple string length example from the picture above

```auto
[{"id":"97198c59935dad84","type":"function","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"$length(str)","func":"// $length(string)\n\nconst example = [];\nexample[0] = \"hello world\";\nexample[1] = [\"test array\"];\nexample[2] = false;\nexample[3] = null;\n\n// Returns the number of characters in the string\nconst expression = [];\nexpression[0] = jsonata(`$length(\"${example[0]}\")`);\nexpression[1] = jsonata(`$length(\"${example[1]}\")`);\nexpression[2] = jsonata(`$length(\"${example[2]}\")`);\nexpression[3] = jsonata(`$length(\"${example[3]}\")`);\n\nmsg.payload = [];\nmsg.payload[0] = expression[0].evaluate();\nmsg.payload[1] = expression[1].evaluate();\nmsg.payload[2] = expression[2].evaluate();\nmsg.payload[3] = expression[3].evaluate();\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"jsonata","module":"jsonata"}],"x":290,"y":120,"wires":[["139d3d075aef541b"]]},{"id":"c4e236b5f403ca46","type":"inject","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":110,"y":120,"wires":[["97198c59935dad84"]]},{"id":"139d3d075aef541b","type":"debug","z":"2b674150e0c6799d","g":"ef756230b8be8f60","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":120,"wires":[]}]

```

Cheers, Chris

---

<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:** [6 March 2024 08:45 UTC](https://discourse.nodered.org/t/jsonata-examples-flows-node-red-org-not-working/86179/2 "2024-03-06T08:45:49Z")

</div>

There are inbuilt RED.util for  
[prepareJSONataExpression](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.prepareJSONataExpression)  
and  
[evaluateJSONataExpression](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.evaluateJSONataExpression)

example

```auto
[{"id":"97198c59935dad84","type":"function","z":"d1395164b4eec73e","name":"$length(str)","func":"// $length(string)\n\nconst example = [];\nexample[0] = \"hello world\";\nexample[1] = [\"test array\"];\nexample[2] = false;\nexample[3] = null;\n\n// Returns the number of characters in the string\nmsg.payload = [];\nexample.forEach((element, index) => {\n let expression = RED.util.prepareJSONataExpression(`$length(\"${example[index]}\")`,node.id);\n RED.util.evaluateJSONataExpression(expression, example, function(err, count) {\n if (err) {\n node.warn(JSON.stringify(err));\n }\n msg.payload[index] = count;\n \n })\n})\n\n\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":570,"y":7580,"wires":[["139d3d075aef541b"]]},{"id":"c4e236b5f403ca46","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":390,"y":7580,"wires":[["97198c59935dad84"]]},{"id":"139d3d075aef541b","type":"debug","z":"d1395164b4eec73e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":7560,"wires":[]}]

```

---

<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:** [20 March 2024 08:46 UTC](https://discourse.nodered.org/t/jsonata-examples-flows-node-red-org-not-working/86179/3 "2024-03-20T08:46:05Z")

</div>

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