Reputation: 155
I am using Django 1.8 and python 2.7. I want to parse some data from my database(sqlite3) to a JSON format.
view.py:
def json_view(request):
maps = Maps.objects.filter(data_id=3).values('ciudad', 'latitud', 'longitud')
userPos = json.dumps(list(maps))
return JsonResponse({'userPosView': userPos})
When I access via browser to my view I get:
{"userPosView": "[{\"latitud\": \"12.1363889\", \"ciudad\": \"Manague\", \"longitud\": \"-86.2513889\"}]"}
The problem is that userPosView looks like the key and the rest as a single value of userPosView
"[{\"latitud\": \"12.1363889\", \"ciudad\": \"Manague\", \"longitud\": \"-86.2513889\"}]".
I would be grateful if somebody could help me because I can not access to the values of latitude and longitude on my JSON object.
Upvotes: 1
Views: 126
Reputation: 77902
JsonResponse()
alreday takes care of encoding it's content to json, so you end up with a double encoding of your data - one in your own code, the second in JsonResponse
. The fix is simple: don't manually encode your data to json before passing it to JsonResponse
:
def json_view(request):
maps = Maps.objects.filter(data_id=3).values('ciudad', 'latitud', 'longitud')
return JsonResponse({'userPosView': list(maps)})
Upvotes: 2
Reputation: 8988
Why are you adding all the JSON inside userPosView?
return JsonResponse({'userPosView': userPos})
You could just return the created dump like this
return JsonResponse(userPos)
Upvotes: 2