DEV Community

Cover image for Django JSON Response safe=False
chryzcode
chryzcode

Posted on

Django JSON Response safe=False

Hiđź‘‹ everyone, I'm happy to see my self writing blogs to you once again after a while due to some reasons.

So, today;

I would be sharing with you shortly, why safe=False is important in rendering {JSON} data in Django.

Firstly, what is JSON?

JSON simply stands for JavaScript Object Notation. It is a standard text-based format for representing structured data based on JavaScript object syntax.

It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

So, driving straight into what safe=False is and its usefulness.

  • Firstly, what is the function of the safe parameter?

The safe parameter here decides on the type of python data type (tuples, dictionaries, strings etc.) you're wanting to pass. So concisely explained, it is the influencer of the data, JSON is to receive and pass across.

  • Secondly, why is it mostly set to False?

The JSON Response in Django set save=True by default, and the safe parameter as a data influencer makes JSON accept the Python Data-Type {Dictionaries} and nothing less. So, at this point any data sent contrary to {Dictionaries} would actually fire up errors.

So, setting the safe parameter to False actually influences JSON to receive any Python Data Type.

Personally and professionally, it is advisable you set it the safe parameter to False because it makes JSON accept both {Dictionaries} and others.

return JsonResponse(<'your python data-type'>, safe = False
Enter fullscreen mode Exit fullscreen mode

Thanks for reading, please kindly comment below for feedback and you can link up with me on Twitter and on this platform for more engagement🤝!⠀

Thanks 🙏

Top comments (2)

Collapse
 
alauddinmondal profile image
Alauddin Mondal

Really you clear the concept like a magic. I am a very big fan of you. Thanks dear. I was confused when I am learning the tutorial on serializer section. Now I explore and got your article very helpful.

Collapse
 
chryzcode profile image
chryzcode

Thank you very much. I'm happy you found it useful