Change Node issue with JSONata

Hi,
I have a issue with the following Node-Red flow :slight_smile:

[{"id":"5f0904d0.61fd5c","type":"tab","label":"tests","disabled":false,"info":""},{"id":"21d27fc5.dbd3c","type":"inject","z":"5f0904d0.61fd5c","name":"Result 1","topic":"","payload":"{\"CollectionsContainer\":[[{\"FirstName\":\"NOMI\",\"PersonAddress\":{\"Country\":\"FR\",\"Department\":\"\"},\"PersonContact\":{\"EmailAddress\":\"NOMI@MAIL.COM\",\"Fax\":\"\",\"Mobile\":\"\",\"Phone\":\"\"},\"PersonLogin\":{\"CodePin1\":\"\",\"CodePin2\":\"\",\"CodePin3\":\"\",\"CodePin4\":\"\",\"Password1\":\"\",\"Password2\":\"\",\"Password3\":\"\",\"Password4\":\"\",\"User1\":\"xxxxxx\",\"User2\":\"\",\"User3\":\"\",\"User4\":\"\"}}]],\"PageNumber\":1,\"PageSize\":1,\"ProcessingTime\":\"PT0.0400035S\",\"TotalNumber\":19996,\"TotalNumberRequest\":0,\"success\":true,\"OperationResult\":{\"Status\":\"SDK_OK\",\"Message\":null,\"SystemMessage\":null,\"StackTrace\":null}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":140,"wires":[["74e774c1.d17acc"]]},{"id":"35963010.0ea44","type":"inject","z":"5f0904d0.61fd5c","name":"Result 0","topic":"","payload":"{\"CollectionsContainer\":[[]],\"PageNumber\":1,\"PageSize\":0,\"ProcessingTime\":\"PT0.0440019S\",\"TotalNumber\":19995,\"TotalNumberRequest\":0,\"success\":true,\"OperationResult\":{\"Status\":\"SDK_OK\",\"Message\":null,\"SystemMessage\":null,\"StackTrace\":null}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":380,"y":180,"wires":[["74e774c1.d17acc"]]},{"id":"74e774c1.d17acc","type":"change","z":"5f0904d0.61fd5c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t   \"topic\": topic,\t   \"count\": $.$count(CollectionsContainer[0]),\t   \"toto\": $.$count(CollectionsContainer[0]) = 0 ? CollectionsContainer[0][0].FirstName : \"\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":160,"wires":[["4508bc09.aea804"]]},{"id":"4508bc09.aea804","type":"debug","z":"5f0904d0.61fd5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":730,"y":160,"wires":[]}]

In the change node, I made a JSONata query:

{
   "topic": topic,
   "count": $.$count(CollectionsContainer[0]),
   "toto": $.$count(CollectionsContainer[0]) = 0 ? CollectionsContainer[0][0].FirstName : ""
}

The goal is to populate "toto", depending the array size of collectionsContainers.

FYI : the JSON data come from a web service o I can not modify it

Thanks

Try this

{ 
   "topic": topic,
   "count": $count(payload.CollectionsContainer[0]),
   "toto": $count(payload.CollectionsContainer[0]) = 0 ? payload.CollectionsContainer[0][0].FirstName : "bah"
}
1 Like

Thanks you; Its ok now.
I understand my mistake. On inject node, the out flow was the "payload" object.
So in input in the change node, I need to parse the payload.
Correct ?

To be honest, I'm not a jsonata expert, I just played with it and decided to try what ended up working :blush:

1 Like