|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/json-custom-python-objects/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/json-custom-python-objects/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fjson-custom-python-objects%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=working-json-data-python |
| Unlock This Lesson | https://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 Python | https://realpython.com/courses/working-json-data-python/ |
| Austin Cepalia | https://realpython.com/courses/working-json-data-python/#team |
| Recommended Tutorial | https://realpython.com/python-json/ |
| Ask a Question | https://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 |
| Contents | https://realpython.com/lessons/json-custom-python-objects/#description |
| Transcript | https://realpython.com/lessons/json-custom-python-objects/#transcript |
| Discussion (3) | https://realpython.com/lessons/json-custom-python-objects/#discussion |
| 00:00 | https://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 otherwise | https://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:14 | https://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 class | https://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:27 | https://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:41 | https://realpython.com/lessons/json-custom-python-objects/#t=41.16 |
| What I’m trying to do here is create a new Person object and immediately | https://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:52 | https://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 is | https://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:05 | https://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.14 | https://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:28 | https://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 know | https://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:53 | https://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 can | https://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:17 | https://realpython.com/lessons/json-custom-python-objects/#t=137.69 |
| Remember, whoever deserializes our JSON on the other end | https://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 on | https://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 an | https://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 object | https://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:07 | https://realpython.com/lessons/json-custom-python-objects/#t=187.18 |
| Remember, the deserialization operation will likely output either a dictionary or | https://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 the | https://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:22 | https://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 end | https://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 recreate | https://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 complex | https://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 complex | https://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, 2019 | https://realpython.com/lessons/json-custom-python-objects/#comment-ca357860-b86b-4bbe-ae2a-568b172b385f |
| June 28, 2019 | https://realpython.com/lessons/json-custom-python-objects/#comment-4cd6b026-0264-4162-9ab9-633d4469b422 |
| Oct. 7, 2020 | https://realpython.com/lessons/json-custom-python-objects/#comment-b3d50a51-9c3b-476a-ba24-42602711501e |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/working-json-data-python/ |
| Overview | https://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 Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover