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.
Python | JSON Equivalent |
---|---|
dict | object |
list , tuple | array |
str | string |
int , float , int | number |
True | true |
False | false |
None | null |
- JSON is short for JavaScript Object Notation, and is a way to store information in an organized, easy-to-access manner.
- 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.
- 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.