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.
Encode Python objects as JSON stringsBasic Usage :
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 nullOptions :
Code :
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}}Examples : Python Dictionaries to JSON strings (sorted by key)
Code :
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));
Output:
{"101": {"Name": "Rohit", "Roll_no": 7, "class": "V"}, "102": {"Name": "David", "Roll_no": 8, "class": "V"}, "103": {"Name": "Samiya", "Roll_no": 12, "class": "V"}}Examples : Python tuple to JSON array
Code :
import json
tup1 = 'Red', 'Black', 'White';
print(json.dumps(tup1));
Output:
["Red", "Black", "White"]Examples : Python list to JSON array
Code :
import json
list1 = [5, 12, 13, 14];
print(json.dumps(list1));
Output :
[5, 12, 13, 14]Examples : Python string to JSON string
Code :
import json
string1 = 'Python and JSON';
print(json.dumps(string1));
Output:
"Python and JSON"Examples : Python Boolean values to JSON Boolean values
Code :
import json
x = True;
print(json.dumps(x));
Output:
trueExamples : Python int, float, int- & float-derived Enums to JSON number
Code :
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-10Decode JSON strings into Python objects
Basic Usage :
json.load(fp,
cls=None,
object_hook=None,
parse_float=None,
parse_int=None,
parse_constant=None,
object_pairs_hook=None, **kw)
The above method deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using the following conversion table.
JSON Python object dict array list string str number (int) int number (real) float true True false False null NoneOptions :
Code :
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}}Examples : JSON array Python tuple
Code :
import json
Json_array = ["Red", "Black", "White"]
print(json.dumps(Json_array));
Output:
["Red", "Black", "White"]Examples : Python list to JSON array
Code:
import json
list1 = [5, 12, 13, 14];
print(json.dumps(list1));
Output:
[5, 12, 13, 14]Examples : JSON string to Python string
Code :
import json
Json_string = "Python and JSON"
print(json.dumps(Json_string));
Output:
"Python and JSON"
Python Version : 3.4
Previous: Working with JSON and JavaScript
Next: Working with JSONPath and JavaScript
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4