Finding a Property in a constantly updating Array

I would like to access an array of 3 values. They are in a property, but during each pass through my flow, a new property is added, containing a new set of 3 values. Basically, I always need to access the final property in the list.

Screenshot 2023-04-20 at 11.40.25

In the screenshot, in this case, I want to locate the ....gx:cord.[89] but on the next loop through the flow it be ....gx:cord[90]. I know how to search and locate a property but not when I dont know the object property anem because it is increasing.

I should add that I dont always start my flow when the property==1. It could already be up to 70 before I read the payload. So I always need to access the last one in the list.

How do I find the last or highest value in the property?

1 Like

Here is a simple flow grabbing the last item in an array. Hopefully it will help you out.

[{"id":"c69aa3d5ec72345b","type":"inject","z":"35590331d81b2967","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Banana\", \"Orange\", \"Apple\", \"Mango\"]","payloadType":"json","x":190,"y":200,"wires":[["b150da40085ebbf3"]]},{"id":"b150da40085ebbf3","type":"function","z":"35590331d81b2967","name":"function 4","func":"let arr = msg.payload    //set arr as the array\nlet arr_len = arr.length //get the length of the array\n// set the payload to the last element in the array\nmsg.payload = arr[arr_len - 1]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":200,"wires":[["ac9640f2921e025c"]]},{"id":"ac9640f2921e025c","type":"debug","z":"35590331d81b2967","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":200,"wires":[]}]

You can also in a change node
set msg. payload
to JSONata J: $$.payload[[-3..-1]]
which should return the last 3 array items.

That very much did help me out.
Took me a few attempts at entering my payload path correctly though!

Thanks but where does my path go int his expression?

Do i leave the rule box to literally, msg. payload

Then enter the expression as;
my path [[-3..-1]]

Sorry but I've never used JSONata before

Your path goes where payload is, so if path is msg.payload["gx:coord"].
it would be

 $$.payload.`gx:coord`[[-3..-1]]

The "gx:cord" has non standard property characters, so needs to be in quotes.
e.g.

[{"id":"61e991d9a076e59b","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload[\"gx:coord\"]","v":"[\"Banana\", \"Orange\", \"Apple\", \"Mango\"]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":500,"wires":[["7e8749bdb091d933"]]},{"id":"7e8749bdb091d933","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":" $$.payload.`gx:coord`[[-3..-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":500,"wires":[["a9ce98808a80655f"]]},{"id":"a9ce98808a80655f","type":"debug","z":"b9860b4b9de8c8da","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":500,"wires":[]}]

My Actual path is;
payload.kml.Folder[0].Folder[0].Placemark[0]["gx:Track"][0]["gx:coord"]

So the path needs to be something like this? I've tried a few variations but I can't seem to get it to work. Can you see what I am doing wrong?
$$.payload.kml.Folder[0].Folder[0].Placemark[0]gx:Track[0]gx:coord[[-3..-1]]

Thanks

You are not following my reply advise about non-standard property name characters and quoting them. You also have missing periods.

$$.payload.kml.Folder[0].Folder[0].Placemark[0]."gx:Track"[0]."gx:coord"[[-3..-1]]

Ah I see, I was using apostrophes instead of quotes, as well as not having the periods.

Thanks that works.

What are the non standard propert characters?

you can use double quotes, single quotes or backticks.

There are many but most are language operators, as in your example : but there are many others
Best to use A-z a-z 0-9 and underscore to avoid the situation.

You can tell there are non-standard chars as the copy path put the object name in square bracket notation, as non-standard chars had been used.

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