Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I parse JSON in Google App Engine?

I'd like to parse a JSON string into an object under Google App Engine (python). What do you recommend? Something to encode/stringify would be nice too. Is what you recommend built in, or a library that I have to include in my app? Is it secure? Thanks.

like image 965
Nogwater Avatar asked Jul 23 '09 13:07

Nogwater


2 Answers

Google App Engine now supports python 2.7. If using python 2.7, you can do the following:

import json structured_dictionary = json.loads(string_received) 
like image 37
speedplane Avatar answered Sep 28 '22 01:09

speedplane


Consider using Django's json lib, which is included with GAE.

from django.utils import simplejson as json  # load the object from a string obj = json.loads( string ) 

The link above has examples of Django's serializer, and here's the link for simplejson's documentation.

If you're looking at storing Python class instances or objects (as opposed to compositions of lists, strings, numbers, and dictionaries), you probably want to look at pickle.

I hope that helps.

Incidentally, to get Django 1.0 (instead of Django 0.96) running on GAE, you can use the following call in your main.py, per this article:

from google.appengine.dist import use_library use_library('django', '1.0') 

Edit: Native JSON support in Google App Engine 1.6.0 with Python 2.7

As of Google App Engine 1.6.0, you can use the Python 2.7 runtime by adding runtime: python27 in app.yaml, and then you can import the native JSON library with import json.

like image 102
Brian M. Hunt Avatar answered Sep 28 '22 00:09

Brian M. Hunt