Python JSON Encoder and Decoder Tutorial with Examples


JSON or Javascript Object Notations is a RFC standard which is used to define complex data types with number 7159.  JSON became very popular in recent years because of its compatibility and simplicity. Python have rich support web applications and popular framework Django is developed with python too. So we generally need to use JSON with python web applications. In this tutorial we will look how to cope with JSON data in Python.

What is JSON

JSON is a structured data expression format. We generally use curly brackets { } , comma ,  and square brackets [ ] in order to create JSON data format. We will look these details in this tutorial later. We will use following example JSON data in  this  tutorial.

Import JSON Library

In python programming language JSON functionalities are provided by json library. We should import this library in order to use these methods. We can import json library with the following line.

Encode JSON

We can express python data structures in different formats. But if we need to send this data to the client side or another part we need to change it into JSON format. We will convert given python data structure into JSON format with the dumps method in the following example.

Decode JSON

In previous step we have converted existing python data structure into JSON format. We will need to do reverse operation too. We need to convert JSON format into python data structures. We will use loads function for this.

LEARN MORE  How to Install Mariadb / Mysql Server in Linux Fedora?

Decode JSON

Decode JSON

Validate JSON From Shell

We can use very complex JSON data in real world examples. JSON is error prone data format. If we need some quick validation about given JSON data we can use python shell to validate JSON data.

In this example we will provide our JSON data by piping into json.tool . Provided JSON data will be parsed and beautified and if there is no error printed to the terminal. If there is an error information about the error is printed to the terminal.

Validate JSON From Shell

Validate JSON From Shell

Data Types

While converting between JSON and Python there is equal data types. We will simply look them in this part.


Javascript objects are expressed as dictionary in Python.


JSON arrays are expressed as list in Python


JSON strings are expressed as str (string) in Python


JSON integers are expressed as int in Python


JSON real numbers are expressed as float in Python.


JSON true boolean is expressed as True in Python.


JSON false boolean is expressed as False in Python.


JSON null boolean is expressed as None in Python.

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)