Python: Convert a list/tuple/dictionary into JSON

Django has a nice serializer library for models, but for a simple way of dumping primitive types straight out to JSON would be to use json.

If you try to use Django's serializer on primitives you'll get this error:

AttributeError: 'str' object has no attribute '_meta'

Instead, use Python's built in library support.

json.dumps(varname);

Python 2.5 has "simplejson", so to make sure you import the right one, just use this import statement.

try:
import json
except ImportError:
import simplejson as json

[ Source ]

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog