Search code examples
pythonjsongoogle-app-enginesimplejson

Simplejson dump and load not returning valid dictionary


I'm trying to store a json result in the GAE datastore, so that I can read it later. I'm dumping it to a String, then storing it, then reading it and loading it back into a dict. But I can no longer read it as a dict after loading.

result = freebase.mqlready(query)

Print result:

[{u'mid': u'/m/095hd',
  u'name': u'Settlers of Catan',
  u'type': u'/games/game'},
 {u'mid': u'/m/025sm93',
  u'name': u'The Game of Life',
  u'type': u'/games/game'}]

 

for r in result:
    name = r.name # works, I can get the name and other values.

json_dump = simplejson.dumps(result)
text = db.Text(json_dump)
fbresult = model.FB(text=text)
fbresult.put()
####
stored_text = fbresult.text
json = simplejson.loads(stored_text)

Print json:

[{u'mid': u'/m/095hd',
  u'name': u'Settlers of Catan',
  u'type': u'/games/game'},
 {u'mid': u'/m/025sm93',
  u'name': u'The Game of Life',
  u'type': u'/games/game'}]

 

for j in json:
    name = json.name 

ERROR:

AttributeError: 'dict' object has no attribute 'name'

Solution

  • Uh, looks like you're accessing the collection rather than the inner object:

    Surely you meant:

    for j in json:
        name = j['name']