I am attempting to convert an XML string to JSON so that I can decode it into a Lua table using :JSONDecode.
I am getting the XML string from running this:
httpservice:GetAsync("https://overpass-api.de/api/map?bbox=-80,40,-80,40")
I’ve tried searching all over but cannot seem to find a way to convert what is returned to JSON. Any ideas?
1 Like
Can we have a snippet of what this API returns, we cant really do anything related to HTTP stuff without knowing what your request returns
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.56.2 b688b00f">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2020-03-23T19:49:02Z"/>
<bounds minlat="37.7803200" minlon="-122.4474200" maxlat="37.7820900" maxlon="-122.4432100"/>
<node id="65287245" lat="37.7766536" lon="-122.4398652" version="5" timestamp="2016-02-20T13:01:51Z" changeset="37328052" uid="14293" user="KindredCoda">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65287251" lat="37.7764448" lon="-122.4415072" version="5" timestamp="2016-12-15T10:23:59Z" changeset="44419545" uid="2644101" user="Chetan_Gowda">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65287254" lat="37.7762357" lon="-122.4431449" version="5" timestamp="2016-02-21T01:27:22Z" changeset="37338249" uid="14293" user="KindredCoda"/>
<node id="65292742" lat="37.7893282" lon="-122.4305663" version="10" timestamp="2016-12-15T10:45:40Z" changeset="44419986" uid="1597155" user="poornibadrinath">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65295345" lat="37.7901653" lon="-122.4239912" version="7" timestamp="2016-02-22T09:39:49Z" changeset="37362523" uid="14293" user="KindredCoda">
<tag k="highway" v="traffic_signals"/>
<tag k="turn_restrictions" v="no"/>
</node>
<node id="65295347" lat="37.7899559" lon="-122.4256361" version="7" timestamp="2016-12-15T13:30:36Z" changeset="44423695" uid="3479270" user="nammala">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65295349" lat="37.7897469" lon="-122.4272800" version="9" timestamp="2019-08-23T17:45:23Z" changeset="73678376" uid="10139505" user="abaker_lyft">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65295352" lat="37.7895373" lon="-122.4289237" version="9" timestamp="2016-02-22T09:41:45Z" changeset="37362555" uid="14293" user="KindredCoda">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65295355" lat="37.7891189" lon="-122.4322094" version="7" timestamp="2016-12-15T13:07:30Z" changeset="44423188" uid="3479270" user="nammala">
<tag k="highway" v="traffic_signals"/>
</node>
<node id="65295359" lat="37.7890165" lon="-122.4330329" version="6" timestamp="2018-02-22T12:58:49Z" changeset="56579491" uid="5288591" user="Irina Karachevtseva"/>
<node id="65299264" lat="37.7694026" lon="-122.4302589" version="8" timestamp="2018-01-21T11:44:51Z" changeset="55625575" uid="933797" user="oba510"/>
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<node id="4784127985" lat="37.7809933" lon="-122.4474873" version="2" timestamp="2019-10-24T15:42:14Z" changeset="76163619" uid="119881" user="clay_c">
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<node id="4784127987" lat="37.7811593" lon="-122.4475181" version="2" timestamp="2019-10-24T15:42:14Z" changeset="76163619" uid="119881" user="clay_c">
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<node id="5128956836" lat="37.7820711" lon="-122.4426767" version="2" timestamp="2018-06-03T08:31:01Z" changeset="59502042" uid="3272286" user="manings"/>
<node id="5128956838" lat="37.7819811" lon="-122.4427013" version="1" timestamp="2017-09-25T17:25:05Z" changeset="52362187" uid="3956517" user="malajul"/>
<node id="5277057886" lat="37.7731552" lon="-122.4222474" version="1" timestamp="2017-12-11T09:09:29Z" changeset="54533672" uid="2115749" user="srividya_c"/>
<node id="5277057887" lat="37.7732433" lon="-122.4222342" version="2" timestamp="2019-03-14T23:28:43Z" changeset="68156046" uid="119881" user="clay_c">
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<node id="5287256274" lat="37.7834346" lon="-122.4241141" version="1" timestamp="2017-12-16T09:37:14Z" changeset="54675116" uid="7168577" user="IcyMidnight"/>
<node id="5287256276" lat="37.7834719" lon="-122.4238212" version="1" timestamp="2017-12-16T09:37:14Z" changeset="54675116" uid="7168577" user="IcyMidnight"/>
<node id="5287256279" lat="37.7835065" lon="-122.4235490" version="1" timestamp="2017-12-16T09:37:14Z" changeset="54675116" uid="7168577" user="IcyMidnight"/>
<node id="5287378560" lat="37.7834236" lon="-122.4242007" version="1" timestamp="2017-12-16T11:20:49Z" changeset="54677000" uid="7168577" user="IcyMidnight">
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<tag k="crossing" v="traffic_signals"/>
<tag k="highway" v="crossing"/>
</node>
<member type="way" ref="431375109" role=""/>
<tag k="fee" v="yes"/>
<tag k="from" v="Munich Street & Geneva Avenue"/>
<tag k="name" v="Muni 43 inbound: The Excelsior => The Marina"/>
<tag k="network" v="Muni"/>
<tag k="nextbus:agency" v="sf-muni"/>
<tag k="nextbus:route" v="43"/>
<tag k="operator" v="San Francisco Municipal Railway"/>
<tag k="payment:cash" v="yes"/>
<tag k="payment:clipper" v="yes"/>
<tag k="payment:prepaid_ticket" v="yes"/>
<tag k="public_transport:version" v="2"/>
<tag k="ref" v="43"/>
<tag k="route" v="bus"/>
<tag k="to" v="Marina Boulevard & Laguna Street"/>
<tag k="type" v="route"/>
</relation>
</osm>
Here is a very small snippet of what it returns. There may be errors/it may be formatted incorrectly because I had to cut out a huge chunk of it.
So, we’ll need a function which takes this data and turns it into a dictionary.
I’m not allowed to show the function as a whole, but I’ll tell you how to turn XML into a dictionary.
First, we need a dictionary (obviously), and this can be created how you would create an array (local dict = {}
)
Next, we’ll need to split each tag into it’s own string so it can be interpreted, the best way of doing this is writing a function that uses recursion to process each tag into the dictionary accordingly.
I myself, do not have the time to write this function as it’s pretty complex
Finally, (assuming this is for Roblox), we can just parse the dictionary into JSONEncode()
1 Like
(Reply deleted by author,temporary message)
If you read the xml snippet, you can see that it is data from OpenStreetMap, an open-source map of streets, buildings, tunnels, etc. It is not leaked. That file titled “map” is the XML file which contains the snippet I posted.
(Reply deleted by author,temporary message)
IIRC the Programming in Lua book has a chapter / section on this.
pil4.pdf (849.0 KB)
Alternatively try reading the source code of these which both do what you want to do:
https://github.com/cloudkick/Agent-Lua-Plugins/blob/master/Json.lua
https://github.com/APItools/middleware/blob/master/middleware/xml-to-json/xml-to-json.lua
I have recently made an XML decoder which can convert XML to a table. The thing is that it puts all nodes under one node table but in the node table it creates a new table for each node with an increasing index. So what you can do is get the node table, then get all of its content and loop through all of them to create them
(I didnt realize this was 4 years ago, time flies, but I didn’t see a solution, so that’s why I made this reply)