Answer the question
In order to leave comments, you need to log in
How to fix sorting in JSON?
When I change the JSON file and save it, it is sorted alphabetically, which is not usual for me.
JSON
{
"a": {
"a": "a",
"g": "g",
"b": "b"
}
}
{
"a": {
"a": "asasas",
"b": "b",
"g": "g"
}
}
with open("file.json") as f:
a = json.load(f)
with open("file.json", "w") as f:
a["a"]["a"] = "asasas"
json.dump(info, f, indent=2, ensure_ascii=False)
Answer the question
In order to leave comments, you need to log in
Firstly, before Python 3.7, there was no concept of the order of keys in the dictionary (I myself was surprised when I found out).
Secondly, there is a parameter in the documentation:
json.dump(... ,sort_keys=False)
There is no way to fix it, the json format does not guarantee that the order of the keys will be preserved.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question