-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67_Python_JSON.py
77 lines (67 loc) · 1.54 KB
/
67_Python_JSON.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Python JSON
print("Python JSON\n")
# JSON in Python
print("JSON in Python:")
import json
# Parse JSON - Convert from JSON to Python
print("\nParse JSON - Convert from JSON to Python")
print("Example:")
# some json
j = '{"name":"Zaber","age":30,"city":"Sylhet"}'
# parsing j
x = json.loads(j)
# result is a python dictionary
print(x)
print(type(x))
print(x["name"])
print(x["age"])
# Converting from Python to JSON
print("\nConvert from Python to JSON")
print("Example:")
# a python object (dict)
d = {"name": "Zaber", "age": 30, "maritalstatus": "unmarried"}
# converting into json
y = json.dumps(d)
# result is a json string
print(y)
print(type(y))
# More examples
print("\nMore examples")
print(json.dumps({"name": "Jahi", "age": 18}))
print(json.dumps(['apple', 'banana', 'mango']))
print(json.dumps(('chess', 'cricket', 'tennis')))
print(json.dumps("This is a string"))
print(json.dumps(25))
print(json.dumps(3.14159))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
# Another example
print("\nAnother example")
c = {
"name": "Zaber",
"age": 30,
"married": False,
"pets": None,
"cars": [{
"brand": "tesla",
"sl no.": 1
}, {
"brand": "marcedes",
"sl no.": 2
}]
}
# print(c)
# converting to json
pytojson = json.dumps(c)
print(pytojson)
print(type(pytojson))
# Formatting the result
print("\nFormatting the result")
print("Example:")
pytojson2 = json.dumps(c, indent=4)
print(pytojson2)
# Ordering the result
print("\nOrdering the result")
pytojson3 = json.dumps(c, indent=2, sort_keys=True)
print(pytojson3)