0

I have created a JSON object that looks like this at source:

 {"34693":"Marko Arnautovic","23122":"Asmir Begovic","39935":"Steven N'Zonzi","4145":"Robert 
Huth","3860":"Jonathan Walters","8505":"Glenn Whelan","23446":"Marc Wilson","29762":"Oussama 
Assaidi","24148":"Erik Pieters","26013":"Mame Biram Diouf","75177":"Marc Muniesa","38772":"Geoff 
Cameron","29798":"Ryan Shawcross","107395":"Jack Butland","3807":"Peter Crouch","8327":"Charlie 
Adam","18181":"Phil Bardsley","254558":"Oliver Shenton","130334":"Adnan 
Januzaj","4092":"Rafael","18701":"Falcao","10620":"Anders Lindegaard","4564":"Robin van 
Persie","25363":"Juan Mata","71174":"Ander Herrera","79554":"David de Gea","71345":"Chris 
Smalling","81726":"Phil Jones","118244":"Luke Shaw","137795":"Tyler Blackett","145271":"James 
Wilson","2115":"Michael Carrick","3859":"Wayne Rooney","8166":"Ashley Young","5835":"Darren
 Fletcher","22079":"Jonny Evans"}

However, when I print this to screen, I get this:

 {u'71174': u'Ander Herrera', u'18701': u'Falcao', u'8505': u'Glenn Whelan', u'118244': u'Luke 
Shaw', u'254558': u'Oliver Shenton', u'34693': u'Marko Arnautovic', u'3807': u'Peter Crouch', 
u'3860': u'Jonathan Walters', u'23122': u'Asmir Begovic', u'75177': u'Marc Muniesa', u'38772': 
u'Geoff Cameron', u'39935': u"Steven N'Zonzi", u'8327': u'Charlie Adam', u'23446': u'Marc Wilson', 
u'81726': u'Phil Jones', u'4145': u'Robert Huth', u'24148': u'Erik Pieters', u'26013': u'Mame Biram Diouf', u'10620': u'Anders Lindegaard', u'71345': u'Chris Smalling', u'2115': u'Michael Carrick', 
u'107395': u'Jack Butland', u'4092': u'Rafael', u'8166': u'Ashley Young', u'18181': u'Phil Bardsley',
 u'145271': u'James Wilson', u'4564': u'Robin van Persie', u'130334': u'Adnan Januzaj', u'25363': 
u'Juan Mata', u'5835': u'Darren Fletcher', u'3859': u'Wayne Rooney', u'79554': u'David de Gea', 
u'29798': u'Ryan Shawcross', u'29762': u'Oussama Assaidi', u'137795': u'Tyler Blackett', u'22079': 
u'Jonny Evans'}

The first few entries look like they are sorted in ascending alphabetical order based on the first name, but then the order seems to disintegrate altogether.

Can anyone tell me:

1) Why this is happening? 2) What syntax I need to print to screen in the original order?

In this example it is not too much of an issue if the JSON object is jumbled up, but I am parsing other much bigger items where printing to screen in a random order makes it impossible to follow.

Thanks

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • 1
    This is a dictionary. Dictionaries are unordered. – Daniel Roseman Jan 05 '15 at 13:47
  • 2
    dictionaries are not ordered in python. Have a look at [this question](http://stackoverflow.com/questions/10206905/how-to-convert-json-string-to-dictionary-and-save-order-in-keys?rq=1) – fredtantini Jan 05 '15 at 13:47
  • Note that the same applies to *JSON objects*; keys are not given any specific order. – Martijn Pieters Jan 05 '15 at 13:48
  • 1
    IMHO this is not a duplicate of the linked question. You can actually keep the order by parsing into an OrderedDict, see http://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python – Daniel Hepper Jan 05 '15 at 14:08
  • @DanielHepper that seems to have done the trick actually. worked on the first dict i tried it on. thanks. – gdogg371 Jan 05 '15 at 14:27

0 Answers0