Array into REST format for API call

Hi All,
Help needed please. I need to turn this array in the payload:

{
    "payload": [
        {
            "latitude": "52.111",
            "longitude": "0.222"
        },
        {
            "latitude": "52.333",
            "longitude": "0.444"
        }
    ]
}

into a format where I can insert it into an http request with all the commas and colons and whatnot as shown below:

https://foobar.com/api/?poly=52.111,0.222:52.333,0.444

Preferably using nodes (even if long to do, this is for teaching purposes)

If not nodes, then Jsonata is better than a vanilla function...

Any bright ideas anyone? Driving me bananas trying to figure it out!

d.

Need more context to give you a "right" answer, but it looks like this is an array of 2 points. Will it always be an array of 2 points? If so, a template node could do the trick.

Try putting https://foobar.com/api/?poly={{payload.0.latitude}},{{payload.0.longitude}},{{payload.1.latitude}},{{payload.1.longitude}} inside a template node.

Ah, good point, no. I reduced it to two points for brevity's sake... it is actually n pairs...where n could be hundreds...

Ah... that's a horse of a different color. Do you need to preserve point order? (I'm assuming this is a point list kind of like an SVG?)

Try this:

[{"id":"c2ff6bfb.9b2a08","type":"debug","z":"d046350b.4cbc2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":220,"wires":[]},{"id":"b17ddf76.3da398","type":"template","z":"d046350b.4cbc2","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"https://foobar.com/api/?poly={{payload}}","output":"str","x":700,"y":220,"wires":[["c2ff6bfb.9b2a08"]]},{"id":"d28a2729.4c81d","type":"split","z":"d046350b.4cbc2","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":310,"y":220,"wires":[["26f845ea.346452"]]},{"id":"26f845ea.346452","type":"template","z":"d046350b.4cbc2","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.latitude}},{{payload.longitude}}","output":"str","x":440,"y":220,"wires":[["d18a3f96.1a2588"]]},{"id":"d18a3f96.1a2588","type":"join","z":"d046350b.4cbc2","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":":","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":570,"y":220,"wires":[["78c37af0.b9e8a4","b17ddf76.3da398"]]}]

It should work for arbitrary array lengths.

I think I may have to preserve point order... its for a Geomapping application.

Thanks for the flow. I'll give it a go.

Look. Poetry and everything.

The flow I gave you should preserve the order. I "aha moment"ed it while sitting in a meeting and expected it to be harder.

Nice solution. Seems to work. Will test further. Thanks a heap!

Really handy!

Just as a follow up. Since I realised that the API I was calling couldn't cope with the number of data points, I reduced the array to a minimum four data points to create a square polygon using a bit of Jsonata.

Set the payload to:

$max($.payload.$number(latitude)) & "," & $min($.payload.$number(longitude)) & ":" &
$max($.payload.$number(latitude)) & "," & $max($.payload.$number(longitude)) & ":" &
$min($.payload.$number(latitude)) & "," & $max($.payload.$number(longitude)) & ":" &
$min($.payload.$number(latitude)) & "," & $min($.payload.$number(longitude))

It could be done with a max/min node I suspect as well.

1 Like