Adapt JSON to worldmap format

Dear Srs, I obtain a JSON data like this:

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

[
  [
    "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:

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

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

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.

[{"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"]]}]

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:

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:

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?

1 Like

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

@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.

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!

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 site). Copy your actual data using the buttons on the debug sidebar, paste them in the tester, and have a go, as they say!

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

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

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

1 Like