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.


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

import json
except ImportError:
import simplejson as json

[ Source ]

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