From the course: Python: XML, JSON, and the Web

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

JSON exception handling

JSON exception handling - Python Tutorial

From the course: Python: XML, JSON, and the Web

Start my 1-month free trial

JSON exception handling

- [Narrator] Just like any other processing that you do in your programs, JSON parsing and serialization can cause errors, and your program should handle them gracefully. So this is accomplished by using the JSON Decode Error class, and it's part of the JSON module. So let's take a look at what happens when there's errors in our JSON code. So I'll open up JSON_error_start.py. And what I'm going to do first is try removing this comma after the list of sandwich toppings. And you can see here that what I've got is a giant string of JSON code that I'm going to try to parse here. And I'm trying to print out the information that's in the structure once I've parsed it into a Python object. So I'm gonna mess around with the string a little bit and show you what happens when the string is malformed. So now that I've removed that comma, let's go ahead and run this. And you can see that my program terminates in a messy stack trace with an indication that they were expecting a comma delimiter on…

Contents