How can I parse JSON in Google App Engine?

PythonJsonGoogle App-Engine

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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)

Solution 3 - Python

Include the simplejson library with your app?

Solution 4 - Python

This is an old question, but I thought I'd give an updated, more detailed answer. For those landing here now, you are almost certainly using python 2.6 or greater, so you can use the built-in json module for Python 2 (or for Python 3, since Google recently added support for Python 3 on GAE). Importing is as easy as import json. Here are some examples of how to use the json module:

import json

# parse json_string into a dict
json_string = '{"key_one": "value_one", "key_two": 1234}'
json_dict = json.loads(json_string)
# json_dict: {u'key_two': 1234, u'key_one': u'value_one'}

# generate json from a dict
json_dict = {'key': 'value', 'key_two': 1234, 'key_three': True}
json_string = json.dumps(json_dict)
# json_string: '{"key_two": 1234, "key": "value", "key_three": true}'

If you are using an older version of python, stick to @Brian M. Hunt's answer.

Again, here is the doc page for the json module for Python 2, and here it is for Python 3.

Solution 5 - Python

If you're using Python2.6 or greater, I've used with success the built-in json.load function. Otherwise, simplejson works on 2.4 without dependencies.

Solution 6 - Python

Look at the python section of json.org. The standard library support for JSON started at python 2.6, which I believe is newer than what the app engine provides. Maybe one of the other options listed?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNogwaterView Question on Stackoverflow
Solution 1 - PythonBrian M. HuntView Answer on Stackoverflow
Solution 2 - PythonspeedplaneView Answer on Stackoverflow
Solution 3 - PythonagiliqView Answer on Stackoverflow
Solution 4 - PythonBrendan GogginView Answer on Stackoverflow
Solution 5 - Pythonc_harmView Answer on Stackoverflow
Solution 6 - PythonYuval FView Answer on Stackoverflow