0

I want to convert my dictionary object as a JSON as i need to send it in ReactJS file. My code is below:

def get(self, request, pk, format = None):
    queryset = SocialAccount.objects.filter(provider='twitter').get(id=pk)
    user_id= queryset.uid
    name = queryset.extra_data['name']
    username = queryset.extra_data['screen_name']
    data = {'user_id':user_id,'name':name,'username':username}
    data = json.dumps(data)
    print(type(json.loads(data)))
    return Response(json.loads(data))

In this view, I got " class 'dict' " in "print(type(json.loads(data)))" line instead of JSON object. If i am going wrong, please guide me. What i need to do? Thanks.

2 Answers2

4

You successfully produced JSON. What you did wrong is that you then decoded the JSON again, back to a Python object, and you only tested the result of that decoding. And that is rightly a new dictionary object; the JSON you produced and stored in data is valid JSON and you have proved it could be decoded again, successfully.

In other words, don't decode again. You already have JSON, just return that:

data = {'user_id':user_id,'name':name,'username':username}
data = json.dumps(data)
return Response(data)

You can simplify this with returning a JsonResponse object instead:

# at the top
from django.http import JsonResponse

# in your view
data = {'user_id':user_id,'name':name,'username':username}
return JsonResponse(data)

This also takes care of setting the right content type for you.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you sir, for help me. but In this, if i printed type(json.dumps(data)), it gives " class 'str' ". –  Nov 01 '17 at 10:06
  • 1
    @Great_DR: yes, a JSON document is stored in a `str` object. That's **correct**. Try printing the value itself. – Martijn Pieters Nov 01 '17 at 10:10
  • yes sir, i am appreciating your answer, but i got extra '/' in all strings. i have checked in postman. So it was not actual JSON object. –  Nov 01 '17 at 10:15
  • @Great_DR: trust the json encoder, and trust Python. The `json.dumps()` function does what it says on the tin, it doesn't add extra characters that were not already there in the input data structure or are needed to make the document valid JSON. Look at what the view returns in a browser, for example, or use an online JSON validator to see that the value really is a valid JSON document. – Martijn Pieters Nov 01 '17 at 10:29
3

You can use JsonResponse instead of Response:

def get(self, request, pk, format=None):
    # ...
    data = {'user_id': user_id,'name': name,'username': username}
    return JsonResponse(data)

Your issue is that you converted your data back to Python object by using json.loads(data). Instead you just need to return Response(data) but you should prefer JsonResponse.

ettanany
  • 19,038
  • 9
  • 47
  • 63
  • 1
    Good answer! Why making it complicated when using a framework like Django. Don't be a fool and use a tool! – cezar Nov 01 '17 at 10:00
  • @cezar yes sir, but i want to use "user_id" in ReactJS file. So i confused to do so. –  Nov 01 '17 at 10:04