In node red using JSONata the output is empty


#1

The input I'm giving is

{ "payload": [ { "FirstName": "Fred", "Surname": "Smith", "Age": 28, "Address": { "Street": "Hursley Park", "City": "Winchester", "Postcode": "SO21 2JN" }, "Phone": [ { "type": "home", "number": "+44 203 5441234" }, { "type": "office", "number": "00 44 1962 001234" }, { "type": "mobile", "number": "0044 7777-001234" } ] }, { "FirstName": "Freda", "Surname": "Smith", "Age": 25, "Address": { "Street": "Hursley Park", "City": "Winchester", "Postcode": "SO21 2JN" }, "Phone": [ { "type": "home", "number": "+44 203 9871234" }, { "type": "office", "number": "00 44 1962 998765" }, { "type": "mobile", "number": "0044 7777-098765" } ] } ] }

The JSONata expression I'm using is
payload.{FirstName & ' ' & Surname:{"Phone":Phone{type:number}}}

The out put displaying while testing is [ { "Fred Smith": { "Phone": { "home": "+44 203 5441234", "office": "00 44 1962 001234", "mobile": "0044 7777-001234" } } }, { "Freda Smith": { "Phone": { "home": "+44 203 9871234", "office": "00 44 1962 998765", "mobile": "0044 7777-098765" } } } ]

But getting the output after injecting message is
{" ":{"Phone":{}}}

The flow is
[{"id":"f97bd0c3.1907a","type":"tab","label":"Flow 9","disabled":false,"info":""},{"id":"7b267b3f.5f0964","type":"inject","z":"f97bd0c3.1907a","name":"","topic":"","payload":"{\"payload\":[{\"FirstName\":\"Fred\",\"Surname\":\"Smith\",\"Age\":28,\"Address\":{\"Street\":\"Hursley Park\",\"City\":\"Winchester\",\"Postcode\":\"SO21 2JN\"},\"Phone\":[{\"type\":\"home\",\"number\":\"+44 203 5441234\"},{\"type\":\"office\",\"number\":\"00 44 1962 001234\"},{\"type\":\"mobile\",\"number\":\"0044 7777-001234\"}]},{\"FirstName\":\"Freda\",\"Surname\":\"Smith\",\"Age\":25,\"Address\":{\"Street\":\"Hursley Park\",\"City\":\"Winchester\",\"Postcode\":\"SO21 2JN\"},\"Phone\":[{\"type\":\"home\",\"number\":\"+44 203 9871234\"},{\"type\":\"office\",\"number\":\"00 44 1962 998765\"},{\"type\":\"mobile\",\"number\":\"0044 7777-098765\"}]}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":240,"wires":[["eb3145fa.7326b8"]]},{"id":"e1a4752d.b7a448","type":"debug","z":"f97bd0c3.1907a","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":350,"y":80,"wires":[]},{"id":"eb3145fa.7326b8","type":"change","z":"f97bd0c3.1907a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.{FirstName & ' ' & Surname:{\"Phone\":Phone{type:number}}}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":160,"y":80,"wires":[["e1a4752d.b7a448"]]}]

Am I missing something.


#2

Looks like your inject node is creating a payload that is an object with one field called "payload".
Try changing your inject node to just include the array of data...


#3

I changed the inject node by removing payload attribute but still the result is same

[{"id":"f97bd0c3.1907a","type":"tab","label":"Flow 9","disabled":false,"info":""},{"id":"7b267b3f.5f0964","type":"inject","z":"f97bd0c3.1907a","name":"","topic":"","payload":"[{\"FirstName\":\"Fred\",\"Surname\":\"Smith\",\"Age\":28,\"Address\":{\"Street\":\"Hursley Park\",\"City\":\"Winchester\",\"Postcode\":\"SO21 2JN\"},\"Phone\":[{\"type\":\"home\",\"number\":\"+44 203 5441234\"},{\"type\":\"office\",\"number\":\"00 44 1962 001234\"},{\"type\":\"mobile\",\"number\":\"0044 7777-001234\"}]},{\"FirstName\":\"Freda\",\"Surname\":\"Smith\",\"Age\":25,\"Address\":{\"Street\":\"Hursley Park\",\"City\":\"Winchester\",\"Postcode\":\"SO21 2JN\"},\"Phone\":[{\"type\":\"home\",\"number\":\"+44 203 9871234\"},{\"type\":\"office\",\"number\":\"00 44 1962 998765\"},{\"type\":\"mobile\",\"number\":\"0044 7777-098765\"}]}]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":240,"wires":[["eb3145fa.7326b8"]]},{"id":"e1a4752d.b7a448","type":"debug","z":"f97bd0c3.1907a","name":"","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","x":600,"y":240,"wires":[]},{"id":"eb3145fa.7326b8","type":"change","z":"f97bd0c3.1907a","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{FirstName & ' ' & Surname:{\"Phone\":Phone{type:number}}}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":240,"wires":[["e1a4752d.b7a448"]]}]


#4

Place a debug node on the output of the inject node. Set the node to display the complete msg object. Deploy and press the inject and look at what comes out.

What do you notice about the data displayed?
What are you trying to set the data to be?


#5

I'm guessing that you are trying to set up your change node this way:

Looks like you just missed the root payload in your expression... don't forget that the JSON body of your inject node will automatically be placed inside msg.payload, which is why you originally had it defined as msg.payload.payload

i added some spaces to your expression for readability, pasted your injected data into the test, and you can see the results are a single object with one property for each person. If my guess is not correct, please show us what else you need.


#6

Thank you ,By accessing through msg.payload in JSONata expression I am able to retrieve the result.