forked from m-bastam/python_class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSON.py
143 lines (131 loc) · 3.89 KB
/
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'''JSON (JavaScript Object Notation) data is roughly the equivalent
of a data dictionary in Python.
JSON is a syntax for storing and exchanging data.
Serialize: Convert an object to a string.
Deserialize: Convert a string to an object.
Python JSON Methods for Serializing and Deserializing JSON Data
Method Purpose
json.dump() Write (serialize) Python data to a JSON file (or stream).
json.dumps() Write (serialize) a Python object to a JSON string.
input: Python Dictionary -> output: JSON string
json.load() Load (deserialize) JSON from a file or similar object.
json.loads() Load (deserialize) JSON data from a string.
input: JSON string -> output: Python Dictionary
'''
import json
# some JSON string:
inp = '{ "name":"John", "age":30, "city":"New York"}'
print(type(inp))
# Convert from JSON to Python::
dic = json.loads(inp)
print(type(dic))
# the result is a Python dictionary:
print(f" age = {dic['age']}, name = {dic['name']} and city = {dic['city']}")
print("\t-------------1-------------\n\n")
# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York",
1 : True
}
print(type(x))
# Convert from Python to JSON:
y = json.dumps(x)
# the result is a JSON string:
print(y)
print(type(y))
print("\t\t------------2--------------\n")
'''
Python and JSON Data Conversions
Python JSON
------ ------
dict object
list, tuple array
str string
int and float number
True true
False false
None null
'''
# Convert Python objects into JSON strings, and print the values
print(json.dumps({"name": "John", "age": 30}))
print(json.dumps(["apple", "bananas"]))
print(json.dumps(("apple", "bananas")))
print(json.dumps("hello"))
print(json.dumps(42))
print(json.dumps(31.76))
print(json.dumps(True))
print(json.dumps(False))
print(json.dumps(None))
print("\t\t------------3--------------\n")
# another example
# x = {
# "name": "John",
# "age": 30,
# "married": True,
# "divorced": False,
# "children": ("Ann","Billy"),
# "pets": None,
# "cars": [
# {"model": "BMW 230", "mpg": 27.5},
# {"model": "Ford Edge", "mpg": 24.1}
# ]
# }
x = {
"firstName": "John",
"lastName": "Smith",
"isAlive": True,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": None
}
# convert into JSON:
y = json.dumps(x)
print(y)
print ("\t\n--------------4--------------\n")
'''You can also define the separators, default value is (". ", ": "),
which means using a dot and a space to separate each object,
and a colon and a space to separate keys from values
sort_keys is used to sort the result alphabetically by keys
'''
y = json.dumps(x, indent = 4, separators=(". ", " = "), sort_keys = True)
# the result is a JSON string:
print(y)
print ("\t\t-------------5---------------\n")
filename = 'example_1.json'
# Open the file (standard file open stuff)
with open(filename, 'w', encoding='utf-8') as f:
json.dump(x,f)
# This is the Excel data (no keys)
filename = 'example_2.json'
# Open the file (standard file open stuff)
with open(filename, 'r', encoding='utf-8', newline='') as f:
# Load the whole json file into an object named products
''' newline='' is used to avoid bringing in the newline character at the
end of each row, which isn’t really part of the data. It’s just a hidden
character to end the line when displaying the data on the screen.
'''
products = json.load(f)
print(products)
print(type(products), len(products))
print(products.keys())
result = list(products.values())
print(result[0]['sport'])
print(result[0]['sport']['q1']['options'][2])