-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path56.json2.py
More file actions
31 lines (28 loc) · 963 Bytes
/
56.json2.py
File metadata and controls
31 lines (28 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 56. Use JSON (JavaScript Object Notation) In Python p2
"""
# The json.dumps() method has parameters to make it easier to read the result with indent
# also I can define the separators, default value is (", ", ": "), which means using a comma
and a space to separate each object, and a colon and a space to separate keys from values:
# The json.dumps() method has parameters to order the keys in the result:
# sort_keys is a parameter to specify if the result should be sorted or not (True,False)
#
"""
import json
# Python Object
all = {
"name":"ali",
"age" : 20,
"married": True,
"divorced": False,
"chlidren":("x","y"),
"pets": None,
"cars":[
{"model":"BMW", "mpg": 27.5},
{"model":"Ford Edge", "mpg": 24.1}
]
}
# Use the indent parameter to define the numbers of indents
# Define the separators
# Use sort_keys
toJSON = json.dumps(all, indent = 2, separators=(";"," :: "), sort_keys=True)
print(toJSON)