Python JSON tricks: how to deal with JSONDecodeError

A trick you may not know yet

Lynn G. Kwong
3 min readMar 16, 2021

JSON (JavaScript Object Notation) is a standard data format that uses human-readable text to store and transmit data objects consisting of attribute-value pairs and array data types. It is a very common data format with a diverse range of applications, especially in web development.

Photo by geralt on Pixabay.

A JSON looks like the dictionary type in Python, but they are different. The essential difference is that a JSON is a pure string that has a strict format. If you don’t write it properly, you may have unexpected errors.

In this statement, student_dict is a valid Python dictionary, but it is not a JSON. To convert a Python dictionary to a JSON, you can use the dumps method of the json module:

--

--

Lynn G. Kwong
Lynn G. Kwong

Written by Lynn G. Kwong

I’m a Software Developer (https://youtube.com/@kwonglynn) keen on sharing thoughts, tutorials, and solutions for the best practice of software development.

No responses yet