3

I have problem in reading json from file using python. The data are stored in dictionary that makes unsorted. I want to store the data in list variable to be in right sequence.

flookup = open('lookup.json') 
self.tags = json.loads(flookup.read())        
flookup.close()

self.tags contains data not ordered based on lookup.json file

{
  "JournalTitle": "Journal Title",
  "PubCode": "Pub Code",
  "UniqueDocumentID": "Unique Document ID"
}
m59
  • 43,214
  • 14
  • 119
  • 136
jerol2k3
  • 35
  • 5
  • 1
    It is difficult to understand what you are asking, but this may help: http://stackoverflow.com/questions/6921699/can-i-get-json-to-load-into-an-ordereddict-in-python – Jim Garrison Dec 12 '13 at 07:03
  • You can load json as a list but it has to be a list. In this case you need to load as a dict and transform to a list. with something like `[v for +, v in myjsondict.items()]` – Callam Delaney Oct 31 '18 at 14:23

1 Answers1

9
import collections, json
data = '{"a":1, "b": 2}'
print json.JSONDecoder(object_pairs_hook=collections.OrderedDict).decode(data)

prints

OrderedDict([(u'a', 1), (u'b', 2)])

(Though it seems a bit strange to require such a thing. If order is significant, JSON data should be in an array.)

Paul Draper
  • 78,542
  • 46
  • 206
  • 285
  • Or, the list of keys being accessed, in those sections of the code where order matters, is fed in from an array (and therefore ordered). – Nisan.H Dec 13 '13 at 01:31