1
+ # json.loads() Parse Example
2
+ import json
3
+
4
+ jsonstring = '''{
5
+ "burrito" : "Steak",
6
+ "double-meat" : true,
7
+ "toppings" : [
8
+ "Black Beans",
9
+ "Lettuce",
10
+ "Salsa",
11
+ "Guacamole"
12
+ ],
13
+ "price" : 9.17
14
+ }'''
15
+
16
+ data = json .loads (jsonstring )
17
+
18
+ print ('Order: ' + data ['burrito' ])
19
+ if (data ['double-meat' ]):
20
+ print ('With Double Meat' )
21
+ for topping in data ['toppings' ]:
22
+ print ('Topping: ' + topping )
23
+ # Order: Steak
24
+ # With Double Meat
25
+ # Topping: Black Beans
26
+ # Topping: Lettuce
27
+ # Topping: Salsa
28
+ # Topping: Guacamole
29
+
30
+ # json.dumps() Serialize Example
31
+ pythondict = {
32
+ 'burrito' : 'Steak' ,
33
+ 'double-meat' : True ,
34
+ 'toppings' : ['Black Beans' ,
35
+ 'Lettuce' ,
36
+ 'Salsa' ,
37
+ 'Guacamole'
38
+ ],
39
+ 'price' : 9.17
40
+ }
41
+
42
+ jsonstring = json .dumps (pythondict , indent = 4 )
43
+
44
+ print ('-------- JSON String Data --------' )
45
+ print (jsonstring )
46
+ # -------- JSON String Data --------
47
+ # {
48
+ # "burrito": "Steak",
49
+ # "double-meat": true,
50
+ # "toppings": [
51
+ # "Black Beans",
52
+ # "Lettuce",
53
+ # "Salsa",
54
+ # "Guacamole"
55
+ # ],
56
+ # "price": 9.17
57
+ # }
58
+
59
+
60
+ # Handling JSON errors with JSONDecodeError
61
+ from json import JSONDecodeError
62
+
63
+ jsonstring = '''{
64
+ "burrito" : "Steak",
65
+ "double-meat" : true,
66
+ "toppings" : [
67
+ "Black Beans",
68
+ "Lettuce"
69
+ "Salsa",
70
+ "Guacamole"
71
+ ],
72
+ "price" : 9.17
73
+ }'''
74
+
75
+ try :
76
+ data = json .loads (jsonstring )
77
+
78
+ print ('Order: ' + data ['burrito' ])
79
+ if (data ['double-meat' ]):
80
+ print ('With Double Meat' )
81
+ for topping in data ['toppings' ]:
82
+ print ('Topping: ' + topping )
83
+
84
+ except JSONDecodeError as error :
85
+ print ('Hold on now, there was a JSON Decoding erroror:' )
86
+ print (error .msg )
87
+ print (error .lineno , error .colno )
88
+ # Hold on now, there was a JSON Decoding erroror:
89
+ # Expecting ',' delimiter
90
+ # 7 13
91
+
92
+ jsonstring = '''{
93
+ "burrito" : "Steak",
94
+ "double-meat" : true,
95
+ "toppings" : [
96
+ "Black Beans",
97
+ "Lettuce",
98
+ "Salsa",
99
+ "Guacamole
100
+ ],
101
+ "price" : 9.17
102
+ }'''
103
+
104
+ try :
105
+ data = json .loads (jsonstring )
106
+
107
+ print ('Order: ' + data ['burrito' ])
108
+ if (data ['double-meat' ]):
109
+ print ('With Double Meat' )
110
+ for topping in data ['toppings' ]:
111
+ print ('Topping: ' + topping )
112
+
113
+ except JSONDecodeError as error :
114
+ print ('Hold on now, there was a JSON Decoding erroror:' )
115
+ print (error .msg )
116
+ print (error .lineno , error .colno )
117
+ # Hold on now, there was a JSON Decoding erroror:
118
+ # Invalid control character at
119
+ # 8 23
120
+
121
+
122
+ # Working With JSON from an API
123
+ import json , requests
124
+
125
+ url = 'http://httpbin.org/json'
126
+ result = requests .get (url )
127
+
128
+ pythondict = result .json ()
129
+
130
+ print (json .dumps (pythondict , indent = 4 ))
131
+
132
+ print (list (pythondict .keys ()))
133
+
134
+ print (pythondict ['slideshow' ]['title' ])
135
+ slides = len (pythondict ['slideshow' ]['slides' ])
136
+ print (f'There are { slides } slides' )
137
+ # {
138
+ # "slideshow": {
139
+ # "author": "Yours Truly",
140
+ # "date": "date of publication",
141
+ # "slides": [
142
+ # {
143
+ # "title": "Wake up to WonderWidgets!",
144
+ # "type": "all"
145
+ # },
146
+ # {
147
+ # "items": [
148
+ # "Why <em>WonderWidgets</em> are great",
149
+ # "Who <em>buys</em> WonderWidgets"
150
+ # ],
151
+ # "title": "Overview",
152
+ # "type": "all"
153
+ # }
154
+ # ],
155
+ # "title": "Sample Slide Show"
156
+ # }
157
+ # }
158
+ # ['slideshow']
159
+ # Sample Slide Show
160
+ # There are 2 slides
0 commit comments