JSONata examples flows.node-red.org not working

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 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.

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

Here is a simple string length example from the picture above

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

There are inbuilt RED.util for
prepareJSONataExpression
and
evaluateJSONataExpression

example

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

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