Search code examples
djangodjango-sessions

Is there simple way to encode json data in Django?


I will get data like this from OpenID provider:

{"identity":"http:\/\/admin.lol.com\/","provider":"http:\/\/lol.com\/server\/",
"name":{"full_name":"\u0421\u0435\u0440\u0433\u0435\u0439 \u0421\u0435\u0440\u0433\u0435\u0439"},
"nickname":"admin","email":"admin@lol.ru","gender":"M","dob":"1985-01-31"}

How to get this data?


Solution

  • Django by default bundles the simplejson library at django.utils.simplejson (http://undefined.org/python/#simplejson). Additionally if you're using python 2.6 or better you can simply import json and you will get the builtin json libraryhttp://docs.python.org/library/json.html. There are also a multitude of other python json libraries, but those two should be vastly more than adequate.

    Once you have decided on a library to use you will simply need to pass your json string to the decoder of that library. For the builtin json library that would be:

    data = json.loads(your_json_string_here)