[Django]-Python dict to JSON via json.loads:

36👍

You are trying to use the wrong method. json.loads is for loading JSON to Python. If you want to convert Python to JSON, you need json.dumps.

result = json.dumps(response[1])

5👍

That dict is in Python dict literal format, not JSON. You can do:

import ast
result = ast.literal_eval(response[1])

to read in the response in that format. Are you sure that Django hasn’t already JSON-decoded the response?

3👍

i have use json on django , i use this :

import simplejson as json
#to encode
final= {'first':first_data,'second':second_data}
json.dumps(final)
#to decode this is the example from python's api 
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')

Leave a comment