1

I want to dump this json to a file:

json.dumps(data) 

This is the data:

 {
       "list":[
        "one": { "id": "12","desc":"its 12","name":"pop"},
        "two": {"id": "13","desc":"its 13","name":"kindle"}
        ]
    }

I want id to be the first property after I dump it to file, but it is not. How can I fix this?

TerryA
  • 58,805
  • 11
  • 114
  • 143
BollMose
  • 3,002
  • 4
  • 32
  • 41

2 Answers2

3

My guess is that it's because you're using a dictionary (hash-map). It's unsortable. What you could do is:

from collections import OrderedDict
data = OrderedDict()
data['list'] = OrderedDict()
data['list']['one'] = OrderedDict()
data['list']['one']['id'] = '12'
data['list']['one']['idesc'] = ...
data['list']['two'] = ...

This makes it sorted by order of input. It's "impossible" to know the output of a dict/hashmap because the nature (and speed) of a traditional dictionary makes the sort/access order vary depending on usage, items in the dictionary and a lot of other factors. So you need to either pass your dictionary to a sort() function prior to sending it to json or use a slower version of the dictionary called OrderedDict (see above).

Many thanks goes out to @MarcoNawijn for checking the source of JSON that does not honor the sort structure of the dictionary, which means you'll have to build the JSON string yourself.

If the parser on the other end of your JSON string honors the order (which i doubt), you could pass this to a function that builds a regular text-string representation of your OrderedDict and formatting the string as per JSON standards. This will however take up more time than I have at this moment since i'm not 100% certain of the RFC for JSON strings.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • This does not work. `json.dump(..)` does not respect the order in which key/value pairs are added. You can easily check this in the interactive interpreter. The `json.dump(..)` function does have a `sort_keys` attribute, but you cannot provide a custom sort function (as far as I can tell without looking at the actual implementation of it). If the actual location of the `id` attribute is important, I would suggest to use the `sort_keys` attribute. The `id` attribute will than at least be at a consistent/predictable location. – Marco Nawijn May 04 '15 at 09:27
  • 1
    So, I checked the implementation of `json.dump(..)`. It indeed hardcodes the sort function to sort on the key: `sorted(dct.items(), key=lambda kv: kv[0])`. – Marco Nawijn May 04 '15 at 09:39
  • @MarcoNawijn Thank you for clairifying this. I didn't think json hardcoded the sort order. Considering the fact that JSON is defined to not be of a specific order on the output end of things, I don't see why this is a relevant question even tho the problem that the user is faced intrigues me :) – Torxed May 04 '15 at 11:43
1

You shouldnt worry about the order in which json is saved. The order will be changed when dumping. Better look at these too. JSON order mixed up and Is the order of elements in a JSON list maintained?

Community
  • 1
  • 1