The trouble I'm having is that the JSON data being returned is a dictionary. Specifically the 'fields' key/value pair is a dictionary of items. Dictionaries are by definition unordered so when I read the JSON object into my Python dictionary it does not preserve the order of the key/value pairs in the 'fields' dictionary. In fact, the object shown in my first post is the dictionary as it looks in Python. If I look at the raw JSON data in the packet, the fields are in the 'expected' order.
The standard, for both Python and JSON (RFC 7159) is that dictionaries are unordered, so I would recommend that in future releases, the data structure of the JSON response be altered to follow the RFC specification. In the meantime, I will work around this.
*Note - Python dictionary types are equivalent to JSON object types. I use dictionary interchangeably above.
**Edit -
So the implementation for this was pretty easy. I'm using Python 2.7.6 and the Requests library which has it's own JSON parser. Instead of using Requests' JSON parser, I used the standard Python library JSON parser instead and passed the object_hook_pairs parameter an OrderedDict type. This resolves my issue.
Code Sample
from collections import OrderedDict
...
# resp.content is the unfiltered data that Requests returns
Thanks for the reply. I ninja edited my post above with the same thing, pretty much. It was a quick and simple change and didn't effect any of my other logic, so that was great!
I should have caught this sooner, but I was relying on a library to do my parsing for me and didn't look at the underlying data structure closely enough.
seancummins
2 Intern
•
226 Posts
1
March 26th, 2014 16:00
bkugler,
Perhaps you could try using a Python OrderedDict instead of a standard dictionary?
e.g.
from collections import OrderedDict
myDictionary = OrderedDict()
Then when you populate myDictionary, the order in which you add keys will be preserved.
Thanks,
- Sean
bkugler
3 Posts
1
March 26th, 2014 16:00
Hi, thanks for the reply.
The trouble I'm having is that the JSON data being returned is a dictionary. Specifically the 'fields' key/value pair is a dictionary of items. Dictionaries are by definition unordered so when I read the JSON object into my Python dictionary it does not preserve the order of the key/value pairs in the 'fields' dictionary. In fact, the object shown in my first post is the dictionary as it looks in Python. If I look at the raw JSON data in the packet, the fields are in the 'expected' order.
The standard, for both Python and JSON (RFC 7159) is that dictionaries are unordered, so I would recommend that in future releases, the data structure of the JSON response be altered to follow the RFC specification. In the meantime, I will work around this.
*Note - Python dictionary types are equivalent to JSON object types. I use dictionary interchangeably above.
**Edit -
So the implementation for this was pretty easy. I'm using Python 2.7.6 and the Requests library which has it's own JSON parser. Instead of using Requests' JSON parser, I used the standard Python library JSON parser instead and passed the object_hook_pairs parameter an OrderedDict type. This resolves my issue.
from collections import OrderedDict
...
# resp.content is the unfiltered data that Requests returns
data=json.loads(resp.content,object_pairs_hook=OrderedDict)
# from here data is now an OrderedDict and we can retrieve keys in the order they were received
bkugler
3 Posts
0
March 26th, 2014 16:00
Hi Sean,
Thanks for the reply. I ninja edited my post above with the same thing, pretty much. It was a quick and simple change and didn't effect any of my other logic, so that was great!
I should have caught this sooner, but I was relying on a library to do my parsing for me and didn't look at the underlying data structure closely enough.
seancummins
2 Intern
•
226 Posts
1
March 26th, 2014 16:00
Awesome; glad you got it working.
Thanks,
- Sean