I am running a filter query in Django and my return results are as follows.
search_result = [{'code': '12345', 'city': 'city1', 'country': 'USA', 'state': 'state1'}, {'code': '15675', 'city': 'city2', 'country': 'USA', 'state': 'state2'}]
I stored this returned data to a dictionary.
data_dict["return_result"] = search_result
data_dict["is_success"] = True
Now I am returning this data_dict as JsonResponse.(bcz this url was called using AJAX).
JsonResponse(data_dict)
In this process I am getting below error -
[{'code': '12345', 'city': 'city1', 'country': 'USA', 'state': 'state1'}, {'code': '15675', 'city': 'city2', 'country': 'USA', 'state': 'state2'}] is not JSON serializable.
Above mentioned data is a NOT a valid Json because of single quotes. If all single quotes are replaced with double quotes then this is a valid json.
Is there any way I can convert it to valid json or search query returns valid json.
Full Stack Trace:
Traceback (most recent call last):
File "/home/rana/DjangoProject/VirtualEnv/e/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/rana/DjangoProject/VirtualEnv/e/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/rana/DjangoProject/VirtualEnv/e/lib/python3.4/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/rana/DjangoProject/Sanstha/sansthaonline/tenant/views/zip_city_search.py", line 37, in search_zip_city
return JsonResponse(context)
File "/home/rana/DjangoProject/VirtualEnv/e/lib/python3.4/site-packages/django/http/response.py", line 505, in __init__
data = json.dumps(data, cls=encoder, **json_dumps_params)
File "/usr/lib/python3.4/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/home/rana/DjangoProject/VirtualEnv/e/lib/python3.4/site-packages/django/core/serializers/json.py", line 115, in default
return super(DjangoJSONEncoder, self).default(o)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: [{'city': 'city1', 'country': 'USA', 'code': '12345', 'state': 'state1'}, {'city': 'city2', 'country': 'USA', 'code': '15675', 'state': 'state2'}] is not JSON serializable