As you know, JSON can be used with many programming languages; it is popularly used with Java, PHP, and Python. In this tutorial, you will learn about the encoding and decoding of JSON objects through Python.



For implementing JSON using Python code, you have to install Demjson in your system. These are the commands to install this:

$tar xvfz demjson-1.6.tar.gz
$cd demjson-(version_Name)
$python setup.py install

Encoding JSON Through Python

The encode() method of Python allows encoding Python object to JSON. Here is a simple program showing the use of JSON through Python.

Example:

import demjson
info = [ { "Alex" : 5000, "Neha" : 4500, "Jadu" : 3000} ]
j_data = demjson.encode(info)
print (j_data)

Decoding JSON Through Python

The decode() method of Python allows decoding Python object to JSON. Here is a simple program showing the implementation:

Example:

import demjson
j_data = '{ "Alex" : 5000, "Neha" : 4500, "Jadu" : 3000}';
info = demjson.decode(j_data)
print  info

Output:

{ "Alex" : 5000, "Neha" : 4500, "Jadu" : 3000}

Here the suffix-u denotes the decoded information of JSON.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram