-2

I made a sorted dictionary to pass through json to a javascript, however, it seems whenever I throw it into a json.dumps() it changes its order into alphabetical by keys. It is critical that I keep it in a certain order or else it will not work. Is there a way to achieve this?

corvid
  • 10,733
  • 11
  • 61
  • 130

1 Answers1

2

Objects in JSON are an unordered collection of key:value pairs (same as dictionaries in Python), so you should use a different data structure if the order is relevant.

A structure like this (in JSON) would work:

{ "columns" : [ "id", "name", "age"],
  "rows" : [[ "john", "John Doe", 42],
            [ "jane", "Jane Miller", 28]
    ]
}
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92