osm-search / Nominatim

Open Source search based on OpenStreetMap data
https://nominatim.org
GNU General Public License v3.0
3.08k stars 712 forks source link

Address detail doesn't preserve order when represented as a JSON object #808

Open EdwardBetts opened 6 years ago

EdwardBetts commented 6 years ago

The JSON specification defines an object is an unordered set of name/value pairs.

The order of the address detail elements returned by nominatim is significant. JSON parsing libraries don't preserve the order of keys in an object.

Here is an example: http://nominatim.openstreetmap.org/details.php?place_id=22349856

{ "artwork":"Order", "road":"Grand Avenue", "commercial":"Meredith Corporation", "city":"Des Moines", "county":"Polk County", "state":"Iowa", "postcode":"50309", "country":"United States of America", "country_code":"us" }

XML is different, the order of tags is preserved by XML libraries.

<artwork>Order</artwork>
<road>Grand Avenue</road>
<commercial>Meredith Corporation</commercial>
<city>Des Moines</city>
<county>Polk County</county>
<state>Iowa</state>
<postcode>50309</postcode>
<country>United States of America</country>
<country_code>us</country_code>

Using a list of lists in JSON would keep the order of the address details. Here is the example reworked.

[
  ["artwork", "Order"],
  ["road", "Grand Avenue"],
  ["commercial", "Meredith Corporation"],
  ["city", "Des Moines"],
  ["county", "Polk County"],
  ["state", "Iowa"],
  ["postcode", "50309"],
  ["country", "United States of America"],
  ["country_code", "us"]
]
lonvia commented 6 years ago

That's a known issue which cannot be solved without breaking existing clients. So something for the next version of the API.

JensHumrich commented 6 years ago

Hey EdwardBetts, could you maybe further explain, why the order is significant? In the example it seems, like every key appears exactly ones. Regards, Jens

EdwardBetts commented 6 years ago

@JensHumrich The elements in the address detail are ordered from most specific to general, ending with the largest geographical unit. JSON parsers will load the address detail into an associative array which isn't guaranteed to maintain the order of the keys.

I hope that answers your questions.