Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Decoding Custom Types From JSON

Give Feedback

In this video, you’ll learn how to deserialize a non-serializable type given in a JSON file.

We can represent a complex object in JSON like this

{
    "__complex__": true,
    "real": 42,
    "imaginary": 36
}

If we let the load() method deserialize this, we’ll get a Python dict instead of our desired complex object. That’s because JSON objects deserialize to Python dict. We can write a custom decoder function that will read this dictionary and return our desired complex object.

def decode_complex(dct):
    if "__complex__" in dct:
        return complex(dct["real"], dct["imaginary"])
    else:
        return dct

Now, we need to read our JSON file and deserialize it. We can use the optional object_hook argument to specify our decoding function.

with open("complex_data.json") as complex_data:
    z = json.load(complex_data, object_hook=decode_complex)

Now, if we print the type of z, we’ll see

<class 'complex'>

We have now deserialized a complex object from a JSON file!

Anonymous on March 30, 2019

This was excellent!! However, it will take me a while to absorb it all. I may have to watch it again.

ChrisF on March 31, 2019

Excellent course, really loved it

Raghunandana SK on April 1, 2019

Yep, This really takes some time to understand . But its explained well for sure

Claudemiro on April 14, 2019

Would be nice to have some examples with nested custom types.

SamR on June 17, 2019

Very helpful course. Thanks!

Fahim on July 9, 2019

Well explained. Thanks

Pygator on Aug. 24, 2019

What do you mean by apis that expose json? Otherwise fantastic course! Can’t wait to try this for my own objects and data.

Austin Cepalia RP Team on Aug. 27, 2019

When I say “APIs that expose JSON”, I’m saying “APIs that return some data in the form of JSON”. For example, if our program needed the ability to see weather forecasts, we could use a weather API. When our program sends a request to the API server, it may return data in JSON format. This course shows you how to interpret JSON data and use it within your Python programs.

Pygator on Aug. 27, 2019

Thanks. Great explanations! JSON has always been so mysterious.

Vincenzo Fiorentini on Oct. 10, 2019

cool. I guess in your example one might also check if complex is true, like cmp=”complex” if cmp in dct and dct[cmp]: return complex(dct[“real”],dct[“imaginary”]) else: return dct

looks like it is ‘false’ and ‘true’ in json instead of False and True. is that so? thanks.

Doug Creighton on Nov. 3, 2019

Is there a course that goes over best practices to convert api string or json to pandas or database structure and than back to json?

Andrew E on Dec. 13, 2019

How would you read in multiple complex numbers? That is, if your data json file looked like this:

{ “z1”:{ “complex”: true, “real”: 46, “complex”: 13, }, “z2”:{ “complex”: true, “real”: 3, “complex”: 4, } }

mikesult on Feb. 20, 2020

Great course, I really liked working with the custom type encoder/decoder. I usually type the exercises out by hand and I have been using single quotes instead of double quotes. One thing I learned was that JSON requires double quotes, single quotes don’t work. You might have mentioned that and I missed it (I think I read that somewhere and already had forgotten it). That made me fumble around for a while when I wrote out the complex_data.json using single quotes and JSON_Ex5.py didn’t work. Those are the kind of things that can trip me a bit. I appreciate learning about working with JSON. Thank you.

rgusaas on March 8, 2020

Good course Austin. It would be helpful if you described an example of when complex types come into play. What are some common uses for complex types and why use them at all?

Also, Links to any references for further study would make this a really solid tutorial.

zeroeum on April 10, 2020

I think the complex number example of non serializable, custom type encoding/decoding wasn’t the best choice. Went from a very useful/trivial example using JSON from API’s straight to a non trivial use case.

Muthukumar Kusalavan on May 7, 2020

Thank you very much for the tutorial, Mr.Austin

Jarrod on May 8, 2020

I am getting an error that I dont understand when trying to do this example.

Traceback (most recent call last): File “c:/Users/jcartwright/Downloads/TEST.PY”, line 16, in <module> z = json.load(complex_data, object_hook=decode_complex) File “C:\Users\jcartwright\AppData\Local\Continuum\anaconda3\lib\json__init__.py”, line 296, in load parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, kw) File “C:\Users\jcartwright\AppData\Local\Continuum\anaconda3\lib\json__init__.py”, line 361, in loads return cls(kw).decode(s) File “C:\Users\jcartwright\AppData\Local\Continuum\anaconda3\lib\json\decoder.py”, line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File “C:\Users\jcartwright\AppData\Local\Continuum\anaconda3\lib\json\decoder.py”, line 355, in raw_decode raise JSONDecodeError(“Expecting value”, s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Jarrod on May 8, 2020

What you have in the video is not what is in the text below it. and I have not been able to get whats in your video to work or the stuff below it. in the video you have loads and below you have load, so whats going on here? How come no one has said anything about this yet?

Ricky White RP Team on May 8, 2020

Hi @cartwrightjarrod. Can you share your code so we can see how we can help you fix your error?

tsusadivyago on May 13, 2020

the custom json type encoding and decoding is a addition for me

datascigit on July 10, 2020

Great Course. It helped me in using datasets via API for my data science projects. Thanks.

Become a Member to join the conversation.