# Adapt JSON to worldmap format

**URL:** <https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937>\
**Category:** General\
**Created:** [30 August 2019 11:57 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937 "2019-08-30T11:57:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zz921286](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zz921286/32/11503_2.png) [@zz921286](https://discourse.nodered.org/u/zz921286)\
**Post date:** [30 August 2019 11:57 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/1 "2019-08-30T11:57:58Z")

</div>

Dear Srs, I obtain a JSON data like this:

```auto
{
  "Nombre": [
    "ITEM0",
    "ITEM1",
    "ITEM2",
    "ITEM3",
    "ITEM4"
  ],
  "Latitud": [
    "43.523224",
    "43.522912",
    "43.523068",
    "43.522916",
    "43.523052"
  ],
  "Longitud": [
    "-5.628755",
    "-5.628594",
    "-5.628626",
    "-5.628744",
    "-5.628809"
  ]
}

```

I need to transform into properly format to inject in a worldmap node. I try to do through JSONATA expression but I only obtain this:

```auto
[
  [
    "ITEM0",
    43.523224,
    -5.628755
  ],
  [
    "ITEM1",
    43.522912,
    -5.628594
  ],
  [
    "ITEM2",
    43.523068,
    -5.628626
  ],
  [
    "ITEM3",
    43.522916,
    -5.628744
  ],
  [
    "ITEM4",
    43.523052,
    -5.628809
  ]
]

```

With this function:

```auto
$zip(Nombre.$string(),Latitud.$number(),Longitud.$number())

```

I'm totally blocked. Could you help me to jump this issue?  
Thanks in advanced.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [30 August 2019 13:30 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/2 "2019-08-30T13:30:08Z")

</div>

I think the JSONata expression is a little more complicated, there are some guru's on this forum, I am not one of them, I would use a function node instead.

example flow.

```auto
[{"id":"f88d2f8e.e4f73","type":"inject","z":"681f8bde.8887bc","name":"","topic":"","payload":"{\"Nombre\":[\"ITEM0\",\"ITEM1\",\"ITEM2\",\"ITEM3\",\"ITEM4\"],\"Latitud\":[\"43.523224\",\"43.522912\",\"43.523068\",\"43.522916\",\"43.523052\"],\"Longitud\":[\"-5.628755\",\"-5.628594\",\"-5.628626\",\"-5.628744\",\"-5.628809\"]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":420,"wires":[["bd1a52.686315b"]]},{"id":"c45b727a.dce65","type":"debug","z":"681f8bde.8887bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":490,"y":420,"wires":[]},{"id":"bd1a52.686315b","type":"function","z":"681f8bde.8887bc","name":"","func":"let m = msg.payload\nlet l = m.Nombre.length\nlet arr = []\n\nfor(x=0;x<m.Nombre.length;x++){\n \n arr.push({\n Nombre:m.Nombre[x],\n Longitud:Number(m.Longitud[x]),\n Latitud:Number(m.Latitud[x])\n })\n \n \n \n}\n\nreturn {payload:arr}","outputs":1,"noerr":0,"x":330,"y":420,"wires":[["c45b727a.dce65"]]}]

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [31 August 2019 17:06 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/3 "2019-08-31T17:06:34Z")

</div>

Kudos for trying to use a JSONata expression! You are close, but you need to use those zipped up arrays to build an object for each data point, like so:

```auto
payload.$zip(Nombre, Latitud, Longitud).{
    "name": $[0],
    "lat": $number($[1]),
    "lon": $number($[2])
}

```

In case you have not seen this yet, you can use the Expression Tester in the `change` node to build your expression while viewing the output -- here's how it looks on my system:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/f8915ca0e6ff2a3b283650650981fb06439f383b.png)

Note that this outputs an array of data point objects -- I checked the worldmap docs and **did not find an example of sending multiple data points** in one payload, so I'm not sure if this will work for you or not... Good luck!

@dceejay, did I just miss the multi-point example, or is an array of points not supported?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [31 August 2019 17:48 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/4 "2019-08-31T17:48:50Z")

</div>

yes you can set an array of points in one go....as per the above.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [31 August 2019 18:55 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/5 "2019-08-31T18:55:07Z")

</div>

@shrickus you are such a jsonata master, do you perhaps have a collection of expressions for certain cases available ? I know that @TotallyInformation has a nice blog with a collection available, would be nice to have this all collected in one spot. Maybe even in the NR documentation.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 August 2019 19:12 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/6 "2019-08-31T19:12:03Z")

</div>

I'm happy for any of mine to be added to something more official. But aren't at least some of these in the Cookbook?

My examples are really just reminders to me of how to do things that I puzzled over - I don't want to have to work it all out again in the future!

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [31 August 2019 19:21 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/7 "2019-08-31T19:21:40Z")

</div>

That's very kind of you to say... Well, it was discussed at one point to have a JSONata example section in the cookbook -- but to be honest I never pursued it.

Most of my answers on the forum are pretty specific to a given input set of data, so I'm not sure how useful it will be to just copy them into some docs. But there are a few common uses, like formatting query results to the ui chart widgets.

To be honest, most of the time I just like to take someone's input data set and see what I can do with it in the jsonata tester. I'm hoping that by showing the results in the forum it will make others want to try it as well -- probably the biggest initial hurdle is just knowing how to use one of the testers (either in the `change` node, or on the [try.jsonata.org](http://try.jsonata.org) site). Copy your actual data using the buttons on the debug sidebar, paste them in the tester, and have a go, as they say!

---

<div class="post-metadata">

**Author:** ![zz921286](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zz921286/32/11503_2.png) [@zz921286](https://discourse.nodered.org/u/zz921286)\
**Post date:** [1 September 2019 14:12 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/8 "2019-09-01T14:12:55Z")

</div>

Dear shrickus and bakman2, both forms work correctly. Thank you very much for your contributions.  
Shrickus I used the "try" screen of the [jsonata.org](http://jsonata.org/) website but could not find the right solution.  
Bakman2 also tried to format the result using a function, however, I didn't know how to use the

> push

statement to increase the array.  
Returning to jsonata, the documentation of the web is totally correct but a section of tricks or tips to polish it and improve its handling would be missing.  
Again, thank you so much.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [1 September 2019 17:15 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/9 "2019-09-01T17:15:34Z")

</div>

> [@zz921286](#):
>
> I didn't know how to use the
> 
> > push
> 
> statement to increase the array.

google 'javascript push w3school' which is where I always go when I need to know how some javascript works.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [2 September 2019 01:03 UTC](https://discourse.nodered.org/t/adapt-json-to-worldmap-format/14937/10 "2019-09-02T01:03:57Z")

</div>

I prefer MDN (Mozilla Developement Network) which is generally more accurate and succinct.
