How to get an ordered list of keys from a json file? #14373
Unanswered
HLammers
asked this question in
Using MicroPython
Replies: 1 comment 2 replies
-
I proposed this by default in #6135 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Let’s say I have a json file with the following structure:
{
"list of objects": {
"key 1": {...},
"key 2": {...},
"key 3": {...},
...
},
...
}
What I need is a list of the keys in ‘list of objects’, in exactly the same order as in the json file.
If I load it this way, I get a randomly ordered list:
import ujson as json
json_file = open('json_file.json')
data = json.load(json_file)
list_of_keys = list(data['list of objects'])
Is there any way to get a list of keys in the original order? I’ve tried using
object_pairs_hook=OrderedDict
, but the MicroPython implementation of json/ujson doesn’t seem to implementobject_pairs_hook
.Beta Was this translation helpful? Give feedback.
All reactions