In Python, the json module provides an API similar to convert in-memory Python objects to a serialized representation known as JavaScript Object Notation (JSON) and vice-a-versa.
json.dump(obj, fp,
skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
cls=None,
indent=None,
separators=None,
default=None,
sort_keys=False, **kw)
The above method serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using the following conversion table.
Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null
import json
student = {"101":{"class":'V', "Name":'Rohit', "Roll_no":7},
"102":{"class":'V', "Name":'David', "Roll_no":8},
"103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
print(json.dumps(student));
Output:
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
"102": {"class": "V", "Name": "David", "Roll_no": 8},
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
import json
student = {"101":{"class":'V', "Name":'Rohit', "Roll_no":7},
"102":{"class":'V', "Name":'David', "Roll_no":8},
"103":{"class":'V', "Name":'Samiya', "Roll_no":12}}
print(json.dumps(student, sort_keys=True));
Ooutput:
{"101": {"Name": "Rohit", "Roll_no": 7, "class": "V"},
"102": {"Name": "David", "Roll_no": 8, "class": "V"},
"103": {"Name": "Samiya", "Roll_no": 12, "class": "V"}}
import json
tup1 = 'Red', 'Black', 'White';
print(json.dumps(tup1));
Output:
["Red", "Black", "White"]
import json
list1 = [5, 12, 13, 14];
print(json.dumps(list1));
Output:
[5, 12, 13, 14]
import json
string1 = 'Python and JSON';
print(json.dumps(string1));
Output:
"Python and JSON"
import json
x = -456;
y = -1.406;
z = 2.12e-10
print(json.dumps(x));
print(json.dumps(y));
print(json.dumps(z));
Output:
-456
-1.406
2.12e-10
Basic usage:
json.load(fp,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None, **kw)
JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None
import json
json_data = '{"103": {"class": "V", "Name": "Samiya", "Roll_n": 12}, "102": {"class": "V", "Name": "David", "Roll_no": 8}, "101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}';
print(json.loads(json_data));
Output:
{"103": {"class": "V", "Name": "Samiya", "Roll_no": 12},
"102": {"class": "V", "Name": "David", "Roll_no": 8},
"101": {"class": "V", "Name": "Rohit", "Roll_no": 7}}
import json
list1 = '[ "Ford", "BMW", "Fiat" ]'
print(json.loads(list1));
import json
Json_string = "Python and JSON"
print(json.dumps(Json_string));
Output:
"Python and JSON"