python - requests module return json with items unordered -


when i'm using python's requests module way:

response = requests.get('http://[some_api_url]') print response.json() different json ordered in contrary viewing json via browser.

for example:
via response.json() get:
[{"key2":"value2"},{"key1:"value1"}]

whereas via browser see supposed be: [{"key1:"value1"},{"key2":"value2"}]

edit: when printing response.text in right order not json

you can use object_pairs_hook argument of json module suggested in doc:

object_pairs_hook optional function called result of object literal decoded ordered list of pairs. return value of object_pairs_hook used instead of dict. feature can used implement custom decoders rely on order key , value pairs decoded (for example, collections.ordereddict() remember order of insertion). if object_hook defined, object_pairs_hook takes priority.

import json collections import ordereddict result = json.loads(request.text,                     object_pairs_hook=ordereddict) 

to simpler, can see in implementation of requests kwargs passed json method json module, hence works well:

d = response.json(object_pairs_hook=ordereddict) 

and d ordereddict order of response.text preserved.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -