Python JSON Encoder and Decoder Tutorial with Examples – POFTUT

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.

{"name":"poftut","age":2,"notes":["a","b","c"]}

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.

import json

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.

json.dumps([{"name":"poftut"},{"age":2},{"notes":("a","b","c")}])

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.

json.loads('[{"name": "poftut"}, {"age": 2}, {"notes": ["a", "b", "c"]}]')
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.

LEARN MORE  How To Control Python For Loop with Break Statement?

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.

$ echo '[{"name": "poftut"}, {"age": 2}, {"notes": ["a", "b", "c"]}]' | python -m json.tool
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.

Object

Javascript objects are expressed as dictionary in Python.

Array

JSON arrays are expressed as list in Python

String

JSON strings are expressed as str (string) in Python

Number(int)

JSON integers are expressed as int in Python

Number(real)

JSON real numbers are expressed as float in Python.

True

JSON true boolean is expressed as True in Python.

False

JSON false boolean is expressed as False in Python.

Null

JSON null boolean is expressed as None in Python.

Leave a Comment