Christian
Christian

Reputation: 1166

Hierarchical data output from App Engine Datastore to JSON?

I have a large hierarchical dataset in the App Engine Datastore. The hierarchy is preserved by storing the data in Entity groups, so that I can pull a whole tree by simply knowing the top element key like so:

query = db.Query().ancestor(db.get(key))

The question: How do I now output this data as JSON and preserve the hierarchy?

Google has a utility class called GqlEncoder that add support for datastore query results to simplejson, but it basically flattens the data, destroying the hierarchy.

Any suggestions?

Upvotes: 1

Views: 1267

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881695

I imagine you're referring to this code and the "flattening" you mention is done by lines 51-52:

   if isinstance(obj, db.GqlQuery):
      return list(obj)

while the rest of the code is fine for your purpose. So, how would you like to represent a GQL query, since you don't what a JS array (Python list) of the objects it contains? It's not clear, besides the entity group (which you're recovering entirely), what gives it hierarchy; is it an issue of "parent"?

Anyway, once that's clarified, copying and editing that file into your own code seems best (it's not designed to let you override just that one tidbit).

Upvotes: 1

Related Questions