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:,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!


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{{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:


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