How To Serialize Python Objects In To JSON Strings?

preview_player
Показать описание
JSON, Javascript Object Notation is a widely used file format for data exchange all across the web thanks to the prevalence of Javascript frameworks. Converting a Python object into a JSON string is called Serialization. In this video, we will see three different ways of serializing Python objects to JSON.

→Timecodes←
---------------------
00:00 Context
00:30 Setup
00:54 The wrong way
01:47 Serializing the Manual Way
02:55 Serializing using an Encoder Function
05:08 Serializing using an Encoder Class
07:08 Closing Remarks

→Links←
-------------

→Social Media←
-----------------------

→Video Summary←
---------------------------
We can serialize Python Objects into JSON strings in multiple ways. First approach is to do this manually. We manually pull our the values from the Python object and put it into a JSON string using f-strings. This is the simplest way but it does not offer easy access to change formatting of the output string.
The second way is to use an encoder function. An encoder function is one which takes our Python object and returns a JSON encodable object. This object then gets serialized by the "dumps" function. As we are using dumps here, we can customize the indentation and formatting. We pass in the encoder function to the dumps function with the keyword default.
The third way is to use an Encoder class. This works similar to an encoder function. We create a class that extends json.JSONEncoder class. In the encoder class we create the default method that has the same content as our encoder function as before.
We pass in the class to dumps using the "cls" keyword. We can use the super class (JSONEncoder) to take care of the TypeError for incompatible types. We can also create an object of the encoder class and pass in the object to that object.
This way, we can serialize a Python object into a JSON string.

→Attributions←
----------------------
(In order)
ERF -"Royalty Free Music from Bensound"
Subscribe Video - Free Stock Video Footage by Videezy

→Tools/Gear←
---------------------
Operating System - Windows 10
Python IDE - Pycharm
Video Editor - Shotcut
Audio Editor - Audacity
Thumbnail Editor - Canva
Рекомендации по теме
Комментарии
Автор

Excellent explanation Swaroop. Useful because of examples. It is Crisp and avoided undue waste of time. Keep it up. Would like to see more videos from you. e.g. Django channels and mobile apps using python ++. Thank you.

narendrasudrik
Автор

Great vid, I just look for serialization and your lesson helped me a lot. Also would be glad seeing more Python lessons from you

ziwkpnf
Автор

Very good presentation with clarity! 🙌🏼

sukanya
Автор

this magical typing is screwing with me haha

ethik
Автор

great video man, it was very helpful, thank you very much!

hmpl
Автор

Thanks! never seen dataclass before. That is very useful!

derhotcenplotz
Автор

great and awesome tutorial about json serialization. thanks.

erick
Автор

Good video, love it!
Helped me a lot

lukasmandl
Автор

Hey can I ask you what camera you are using? The quality is really good and I wonder which one it could be? :) Great content thanks for that! :)

theitalian
Автор

Hi, please help with link to your video for 'deserialisation'. i.e. converting json back to python object of a class. Thank you.

narendrasudrik
Автор

What about nested objects? Do I have to create an encoder manually for each class?

maykonstinglin
Автор

Hey Swaroop. In school, my teacher used your class Encoder at 5:11 for a Flask app. However, in that app, they attached the encoder to the instance of the Flask app itself (app.json_encoder = JSONEncoder). Have you heard of it being used this way, and do you understand how it works?

getoverhere
Автор

You did not answer your own question. With any of these approaches how can you scale to handle complex and nested objects? Thanks!

hussamqasem
Автор

how I can serialize multiple files which are retrieved from the database.

taddelepetros
Автор

Did the Deserialization video ever come out?

tim
Автор

please can I know the meaning of the jsonified_p that you use, it's the only point that i need to finish my encoding..
hope you see it very soon, thanks for the video.

ndiayejacqueline
Автор

Feels like the encoding method should be part of the class. Also as dataclasses have an as_dict() method they can be passed direct to dumps so the rest of this is unnecessary.

EdFrench_uk
Автор

thankyou, would you help me teach python 1 to 1..?..we can have a quick chat if intrstd, pls let me knw..

letsbye
Автор

another reason to hate this should not be soo complicated... Can you try this example with some datetime fields please.

berkanbilgin
Автор

why are you running while explaining , go slow.... i can't understand...

kumaranda