0

I use ajax to get a JSON object from an external API. Here is the JSON object that I get :

"pricing_options": {
            "7": {
                "lodging_id": 7,
                "lodging_url": "https://exampleapiurl.com/7",
                "price": 520.0,
                "name": "Private Room w/EnSuite Bath",
                "max_occupancy": 2,
                "image_url": "https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
            },
            "552": {
                "lodging_id": 552,
                "lodging_url": "https://exampleapiurl.com/552",
                "price": 400.0,
                "name": "Private Room w/Queen Bed & Hall Bath",
                "max_occupancy": 2,
                "image_url": "https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
            },
            "80": {
                "lodging_id": 80,
                "lodging_url": "https://exampleapiurl.com/80",
                "price": 400.0,
                "name": "Private Room w/Twin Bed & Hall Bath",
                "max_occupancy": 1,
                "image_url": "https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
            }
        }

However, it keeps re-ordered automatically by its ID (7, 80, 552). I want to keep it that way (in the same exact order).

  • The ordering is implementation specific, you cannot rely on other JSON parsers to interpret it in the same order. – zr0gravity7 Jul 14 '21 at 16:30
  • Possibly a duplicate of https://stackoverflow.com/questions/5020699/how-do-you-stop-chrome-and-opera-sorting-json-objects-by-index-asc – Tim Sheehan Jul 14 '21 at 16:40
  • 1
    Does this answer your question? [How do you stop Chrome and Opera sorting JSON objects by Index ASC?](https://stackoverflow.com/questions/5020699/how-do-you-stop-chrome-and-opera-sorting-json-objects-by-index-asc) – Tim Sheehan Jul 14 '21 at 16:41

1 Answers1

0

The JSON protocol does not define an ordering for dictionary entries. As such, in all implementations it will be impossible to set or preserve a specific order.

If you want to set an order, modify the JSON schema so that the structure containing these entries is a list instead. For example:

{
   "pricing_options":[
      {
         "lodging_id":7,
         "lodging_url":"https://exampleapiurl.com/7",
         "price":520.0,
         "name":"Private Room w/EnSuite Bath",
         "max_occupancy":2,
         "image_url":"https://exampleapiurl.com/assets/282/2017/09/Karuna.jpg"
      },
      {
         "lodging_id":80,
         "lodging_url":"https://exampleapiurl.com/80",
         "price":400.0,
         "name":"Private Room w/Twin Bed & Hall Bath",
         "max_occupancy":1,
         "image_url":"https://exampleapiurl.com/assets/282/2017/09/Basement-4-e1624825910642.jpg"
      },
      {
         "lodging_id":552,
         "lodging_url":"https://exampleapiurl.com/552",
         "price":400.0,
         "name":"Private Room w/Queen Bed & Hall Bath",
         "max_occupancy":2,
         "image_url":"https://exampleapiurl.com/assets/282/2019/03/Khandro-300x240.jpg"
      },
   ]
}

Alternatively, parse the JSON and build an internal data structure that maintains the entries in a specific order.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
  • Unfortunately, I can't modify the structure because it came from 3rd party service and we're using their API to fetch the data. Is there a way to keep it as is? When I access the API URL directly with Chrome, it showing the correct order (7, 552, 80). However, when I see the logs on the Chrome console, it's being sorted by the ID (7, 80, 552). – Hendra Setiawan Jul 14 '21 at 16:30
  • Not that I know of. Why do you need a specific ordering? Why can you not have a layer in your application that parses this JSON and produces another JSON in your preferred format? If you simply want to view a JSON payload in your browser's native JSON viewer, then AFAIK you will be limited by the implemented behavior (again, not defined by the protocol) of that browser. – zr0gravity7 Jul 14 '21 at 20:19
  • I need to keep the order exactly the same as what it has on the JSON object so that I can make an exact copy of data between the API and my website. – Hendra Setiawan Jul 15 '21 at 01:58
  • The rendered order does not need to be the same for the JSONs to be identical. As an analogy, sets in mathematics are unordered unique collections of elements. They are denoted by curly brackets with items inside. The sets `{1, 2, 3, 4}` and `{1, 4, 2, 3}` are identical in every mathematical sense, despite being denoted differently. The same principle applies to JSONs. If you want to elaborate on your use case then I might be able to understand better why you feel you need to do this. – zr0gravity7 Jul 15 '21 at 05:07