Python JSON

JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays. It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers.

JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner. 

A common use of JSON is to read data from a web server, and display the data in a web page.

JSON is a data representation format used for:

  • Storing data (Client/Server)
  • Exchanging data between Client and Server

And now a days Django is rising technology for web-development, which is based on python . So the JSON library of python is a powerful module/tool for web developers.

To use JSON in your Python code, the first thing we must do is import the JSON library with the entry:

import json

Consider a small example

import json

#key:value pair
employee ={"name":"Surya",
            "idNum": 'SG1280',
	     "Salary":25000}

# conversion to JSON done by dumps() function
show = json.dumps(employee)

print(show)
{"name": "Surya", "idNum": "SG1280", "Salary": 25000}

What is JSON dumps() ?

The JSON. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to json. The JSON. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.

Examples using dumps() method

import json

# string conversion to String
print(json.dumps("Hello"))

# int conversion to Number
print(json.dumps(123456789))

# float conversion to Number
print(json.dumps(55.43))

# list conversion to Array
print(json.dumps(['Welcome to fullstackadda.com']))

# tuple conversion to Array
print(json.dumps(("Welcome", "to", "fullstackadda.com")))

"Hello"
123456789
55.43
["Welcome to fullstackadda.com"]
["Welcome", "to", "fullstackadda.com"]

Here’s a table showing Python objects and their equivalent conversion to JSON.

PythonJSON Equivalent
dictobject
list, tuplearray
strstring
int, float, intnumber
Truetrue
Falsefalse
Nonenull
Python JSON
  1. JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner.
  2. JSON. dumps() method allows us to convert a python object into an equivalent JSON object. Or in other words to send the data from python to JSON.
  3. JSON. dump() method allows us to convert a python object into an equivalent JSON object and store the result into a JSON file at the working directory.