Table of Contents:
- Chapter 8.5 The
json
module
- JSON (JavaScript Object Notation) is a light weight data interchange format inspired by
Javascript
object literal syntax. - A JSON file contains an object or an array that can contain different levels of nesting.
- Keys are
string
and are surrounded with double quotes"
.
SN | JSON Data Type | JSON Example | Python Equivalent | Python Example |
---|---|---|---|---|
1 | number | 1 , 2.5 |
int or float |
1 , 2.5 |
2 | string | "John" |
str |
'john' |
3 | boolean | true or false |
bool |
True or False |
4 | object | {"key": "value"} |
dict |
{'key': 'value'} |
5 | arrray | [1,2,3] |
list , tuple |
[1,2,3] |
6 | null | null |
None |
None |
{
"name": "John Doe",
"age": 20,
"married": true,
"children": null
}
{
"name": "Grade 1",
"session": 2022,
"class_teacher": "John Lennon",
"students": [
{
"id": 1,
"name": "John Doe",
"subjects": [
"Physics",
"Mathematics"
]
},
{
"id": 2,
"name": "Jane Doe",
"subjects": [
"Chemistry",
"Biology"
]
}
]
}
person = {
"name": "John Doe",
'age': 20,
'married': False,
'occupation': 'programmer',
'programming_languages': [
{
"name": "Python",
"version": 3.9,
'level': "Beginner",
},
{
"name": "Java",
"version": 17,
'level': "Expert",
},
{
"name": "C++",
"version": 2019,
'level': "Mid",
},
]
}
We can dump the python dictionary or a list into JSON string using json.dumps()
method.
import json
json_str = json.dumps(person)
We can dump the python dictionary or a list into JSON file using json.dump()
method.
To dump the dictionary into the json file, we need to open file in a write mode
.
import json
with open("filename.json", 'w') as file:
json.dump(person, file)
The above command saves the output in the file filename.json
inside the current working directory.
we can load the JSON string into python equivalent data using json.loads()
method.
import json
json_string = '{"name": "John", "age": 20}'
dict_1 = json.loads(json_string)
we can load the JSON file into python equivalent data using json.load()
method.
To load the dictionary from the json file, we need to open file in a read mode
.
import json
with open("filename.json", 'r') as file:
dict_1 = json.load(file)