René's URL Explorer Experiment


Title: JSON for Custom Python Objects (Video) – Real Python

Open Graph Title: JSON for Custom Python Objects – Real Python

Description: Welcome back to our series on working with JSON data in Python. In this video, we’re going to learn how to encode custom Python objects or otherwise non-serializable types into JSON format. I’m here in Visual Studio Code, and I’ve already typed out…

Open Graph Description: Welcome back to our series on working with JSON data in Python. In this video, we’re going to learn how to encode custom Python objects or otherwise non-serializable types into JSON format. I’m here in Visual Studio Code, and I’ve already typed out…

Opengraph URL: https://realpython.com/lessons/json-custom-python-objects/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "JSON for Custom Python Objects",
    "description": "Welcome back to our series on working with JSON data in Python. In this video, we’re going to learn how to encode custom Python objects or otherwise non\u002Dserializable types into JSON format. I’m here in Visual Studio Code, and I’ve already typed out…",
    "thumbnailUrl": ["https://files.realpython.com/media/Working-With-JSON-Data-in-Python_Watermarked.66a8fdcb8859.jpg"],
    "uploadDate": "2019-03-28T14:00:00+00:00",
    "duration": "PT4M18S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/json-custom-python-objects/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/Working-With-JSON-Data-in-Python_Watermarked.66a8fdcb8859.jpg
og:imagehttps://files.realpython.com/media/Working-With-JSON-Data-in-Python_Watermarked.66a8fdcb8859.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/json-custom-python-objects/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/json-custom-python-objects/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fjson-custom-python-objects%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=working-json-data-python
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=working-json-data-python
https://realpython.com/courses/working-json-data-python/#team
Working With JSON in Pythonhttps://realpython.com/courses/working-json-data-python/
Austin Cepaliahttps://realpython.com/courses/working-json-data-python/#team
Recommended Tutorialhttps://realpython.com/python-json/
Ask a Questionhttps://realpython.com/lessons/json-custom-python-objects/#discussion
https://realpython.com/feedback/survey/course/working-json-data-python/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/working-json-data-python/disliked/?from=lesson-title
Contentshttps://realpython.com/lessons/json-custom-python-objects/#description
Transcripthttps://realpython.com/lessons/json-custom-python-objects/#transcript
Discussion (3)https://realpython.com/lessons/json-custom-python-objects/#discussion
00:00https://realpython.com/lessons/json-custom-python-objects/#t=0.51
Welcome back to our series on working with JSON data in Python. In this video,https://realpython.com/lessons/json-custom-python-objects/#t=0.51
we’re going to learn how to encode custom Python objects or otherwisehttps://realpython.com/lessons/json-custom-python-objects/#t=5.58
non-serializable types into JSON format.https://realpython.com/lessons/json-custom-python-objects/#t=10.38
00:14https://realpython.com/lessons/json-custom-python-objects/#t=14.73
I’m here in Visual Studio Code,https://realpython.com/lessons/json-custom-python-objects/#t=14.73
and I’ve already typed out a bit of code here to save some time.https://realpython.com/lessons/json-custom-python-objects/#t=16.92
I’ve got a generic Person classhttps://realpython.com/lessons/json-custom-python-objects/#t=20.58
and now I want to serialize a Person object into a string.https://realpython.com/lessons/json-custom-python-objects/#t=22.83
00:27https://realpython.com/lessons/json-custom-python-objects/#t=27.15
I’ll move down to the bottom here and I’ll type json_str = json.dumps(Person())https://realpython.com/lessons/json-custom-python-objects/#t=27.15
and I’ll give him a name of "Will"https://realpython.com/lessons/json-custom-python-objects/#t=36.67
and an age of 29.https://realpython.com/lessons/json-custom-python-objects/#t=38.82
00:41https://realpython.com/lessons/json-custom-python-objects/#t=41.16
What I’m trying to do here is create a new Person object and immediatelyhttps://realpython.com/lessons/json-custom-python-objects/#t=41.16
serialize that into a string.https://realpython.com/lessons/json-custom-python-objects/#t=45.03
Now I’ll simply print the JSON-formatted string to the console.https://realpython.com/lessons/json-custom-python-objects/#t=47.49
00:52https://realpython.com/lessons/json-custom-python-objects/#t=52.29
So if I right-click here and I choose Run Code,https://realpython.com/lessons/json-custom-python-objects/#t=52.29
we’ll see that we get a TypeError. Python is telling us that our Person object ishttps://realpython.com/lessons/json-custom-python-objects/#t=55.95
not serializable. Let’s take a look at why this is happening.https://realpython.com/lessons/json-custom-python-objects/#t=61.29
01:05https://realpython.com/lessons/json-custom-python-objects/#t=65.67
Unfortunately for us,https://realpython.com/lessons/json-custom-python-objects/#t=65.67
the built-in dumps() module only knows how to serialize built-in types.https://realpython.com/lessons/json-custom-python-objects/#t=66.99
That’s why we can say something like json.dumps() and pass in 3.14https://realpython.com/lessons/json-custom-python-objects/#t=72.27
and it will have no problem. But some types are not so easily serializable,https://realpython.com/lessons/json-custom-python-objects/#t=78.33
like the Python complex type or any other custom type that we create.https://realpython.com/lessons/json-custom-python-objects/#t=83.31
01:28https://realpython.com/lessons/json-custom-python-objects/#t=88.32
The complex type is used to store complex numbers,https://realpython.com/lessons/json-custom-python-objects/#t=88.32
which are numbers with an imaginary component.https://realpython.com/lessons/json-custom-python-objects/#t=91.86
This arises when we try to take the square root of a negative number—https://realpython.com/lessons/json-custom-python-objects/#t=94.8
but honestly, that just brings back bad memories from high school math class,https://realpython.com/lessons/json-custom-python-objects/#t=98.64
so let’s not talk too much about it. What’s important here is that you knowhttps://realpython.com/lessons/json-custom-python-objects/#t=103.2
the complex type is non-serializable, just as our custom Person type is.https://realpython.com/lessons/json-custom-python-objects/#t=107.7
01:53https://realpython.com/lessons/json-custom-python-objects/#t=113.22
But we still want to store these objects in our JSON files,https://realpython.com/lessons/json-custom-python-objects/#t=113.22
so what gives? That’s where simplification comes in, and I already like the name.https://realpython.com/lessons/json-custom-python-objects/#t=116.9
What we need to do is break the object down into simpler parts that canhttps://realpython.com/lessons/json-custom-python-objects/#t=123.74
individually be serialized. We should ask ourselves:https://realpython.com/lessons/json-custom-python-objects/#t=128.479
what is the minimum amount of information necessary to recreate this object?https://realpython.com/lessons/json-custom-python-objects/#t=132.26
02:17https://realpython.com/lessons/json-custom-python-objects/#t=137.69
Remember, whoever deserializes our JSON on the other endhttps://realpython.com/lessons/json-custom-python-objects/#t=137.69
might not know anything about our Person object,https://realpython.com/lessons/json-custom-python-objects/#t=141.77
so we need to make sure that we serialize everything that’s needed. From here onhttps://realpython.com/lessons/json-custom-python-objects/#t=144.62
out, I’m going to use the complex type as an example of how to serialize anhttps://realpython.com/lessons/json-custom-python-objects/#t=149.0
otherwise non-serializable type. First,https://realpython.com/lessons/json-custom-python-objects/#t=153.92
we have to understand how the complex type works. It’s got two parts:https://realpython.com/lessons/json-custom-python-objects/#t=158.03
a real part and an imaginary part. In the diagram on the right here,https://realpython.com/lessons/json-custom-python-objects/#t=162.53
6 + 2j means that 6.0 is our real part,https://realpython.com/lessons/json-custom-python-objects/#t=167.57
and 2.0 is our imaginary part. Fortunately,https://realpython.com/lessons/json-custom-python-objects/#t=172.49
each of those numbers can be serialized,https://realpython.com/lessons/json-custom-python-objects/#t=176.81
and as far as we’re concerned right now,https://realpython.com/lessons/json-custom-python-objects/#t=179.02
that’s all we need to recreate our complex objecthttps://realpython.com/lessons/json-custom-python-objects/#t=181.24
after the JSON has been deserialized.https://realpython.com/lessons/json-custom-python-objects/#t=184.54
03:07https://realpython.com/lessons/json-custom-python-objects/#t=187.18
Remember, the deserialization operation will likely output either a dictionary orhttps://realpython.com/lessons/json-custom-python-objects/#t=187.18
a list,https://realpython.com/lessons/json-custom-python-objects/#t=192.73
so whoever deserializes it is most likely only going to get a list with thehttps://realpython.com/lessons/json-custom-python-objects/#t=193.54
numbers 6 and 2 in it, and nothing else.https://realpython.com/lessons/json-custom-python-objects/#t=198.49
03:22https://realpython.com/lessons/json-custom-python-objects/#t=202.09
Once we have the data from our original object,https://realpython.com/lessons/json-custom-python-objects/#t=202.09
we can recreate that object by passing the data into the necessary constructor.https://realpython.com/lessons/json-custom-python-objects/#t=204.79
So in this example here,https://realpython.com/lessons/json-custom-python-objects/#t=210.34
pretend that we’ve serialized the complex number 3 + 8j. Of course,https://realpython.com/lessons/json-custom-python-objects/#t=212.26
we can’t actually serialize the complex object in that form,https://realpython.com/lessons/json-custom-python-objects/#t=217.84
so we just serialized 3 and 8. The person on the other endhttps://realpython.com/lessons/json-custom-python-objects/#t=221.56
just deserialized our JSON,https://realpython.com/lessons/json-custom-python-objects/#t=226.09
and now they’ve got a list of [3, 8] to work with. In order to recreatehttps://realpython.com/lessons/json-custom-python-objects/#t=228.61
the original object,https://realpython.com/lessons/json-custom-python-objects/#t=233.56
all they need to do is pass those numbers into the constructor for the complexhttps://realpython.com/lessons/json-custom-python-objects/#t=235.09
class. And as you can see on screen,https://realpython.com/lessons/json-custom-python-objects/#t=239.62
that gives them a complex object with the same value as our original complexhttps://realpython.com/lessons/json-custom-python-objects/#t=242.41
object. There’s one more little hiccup we’ll see later on,https://realpython.com/lessons/json-custom-python-objects/#t=247.27
but for now,https://realpython.com/lessons/json-custom-python-objects/#t=251.38
let’s see how we can handle encoding the complex type within our code.https://realpython.com/lessons/json-custom-python-objects/#t=252.43
May 11, 2019https://realpython.com/lessons/json-custom-python-objects/#comment-ca357860-b86b-4bbe-ae2a-568b172b385f
June 28, 2019https://realpython.com/lessons/json-custom-python-objects/#comment-4cd6b026-0264-4162-9ab9-633d4469b422
Oct. 7, 2020https://realpython.com/lessons/json-custom-python-objects/#comment-b3d50a51-9c3b-476a-ba24-42602711501e
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/working-json-data-python/
Overviewhttps://realpython.com/courses/working-json-data-python/
https://realpython.com/lessons/encoding-custom-types-json/
What Is JSON? 04:48 https://realpython.com/videos/what-is-json/
Serializing JSON Data 04:39 https://realpython.com/videos/serializing-json-data/
Deserializing JSON Data 03:04 https://realpython.com/lessons/deserializing-json-data/
Working With JSON Data 10:34 https://realpython.com/lessons/working-json-data-python/
JSON for Custom Python Objects 04:18 https://realpython.com/lessons/json-custom-python-objects/
Encoding Custom Types to JSON 04:02 https://realpython.com/lessons/encoding-custom-types-json/
Decoding Custom Types From JSON 04:59 https://realpython.com/lessons/decoding-custom-types-json/
Working With JSON Data in Python (Quiz) 06:00 https://realpython.com/lessons/working-with-json-data-in-python-quiz/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.