# Array into REST format for API call

**URL:** <https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850>\
**Category:** General\
**Created:** [12 November 2019 17:16 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850 "2019-11-12T17:16:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [12 November 2019 17:16 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/1 "2019-11-12T17:16:07Z")

</div>

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

```auto
{
    "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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [12 November 2019 18:11 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/2 "2019-11-12T18:11:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [12 November 2019 18:23 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/3 "2019-11-12T18:23:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [12 November 2019 18:33 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/4 "2019-11-12T18:33:41Z")

</div>

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?)

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [12 November 2019 18:38 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/5 "2019-11-12T18:38:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [12 November 2019 18:42 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/6 "2019-11-12T18:42:44Z")

</div>

> [@JayDickson](#):
>
> 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?)

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.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [12 November 2019 18:44 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/7 "2019-11-12T18:44:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [12 November 2019 18:45 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/8 "2019-11-12T18:45:02Z")

</div>

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

Really handy!

---

<div class="post-metadata">

**Author:** ![danleighton](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danleighton/32/7711_2.png) [@danleighton](https://discourse.nodered.org/u/danleighton)\
**Post date:** [13 November 2019 00:34 UTC](https://discourse.nodered.org/t/array-into-rest-format-for-api-call/17850/9 "2019-11-13T00:34:46Z")

</div>

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:

```auto
$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.
