-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path9
More file actions
330 lines (227 loc) · 9 KB
/
9
File metadata and controls
330 lines (227 loc) · 9 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# DAY-9 | JSON (JavaScript Object Notation) manipulation in Python
JSON (JavaScript Object Notation) manipulation in Python involves working with JSON data, which is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate.
In Python, you can manipulate JSON data using the `json` module, which provides methods for encoding and decoding JSON data. Here are some common JSON manipulation tasks in Python:
1. **Loading JSON Data:**
- The `json.loads()` function is used to parse a JSON string and convert it into a Python data structure (usually a dictionary or a list).
```python
import json
json_string = '{"name": "John Doe", "age": 30, "city": "New York"}'
data = json.loads(json_string)
print(data)
```
2. **Reading JSON from a File:**
- The `json.load()` function is used to read JSON data from a file and convert it into a Python data structure.
```python
import json
with open('data.json', 'r') as file:
data = json.load(file)
print(data)
```
3. **Dumping JSON Data:**
- The `json.dumps()` function is used to convert a Python object (usually a dictionary or a list) into a JSON formatted string.
```python
import json
data = {'name': 'John Doe', 'age': 30, 'city': 'New York'}
json_string = json.dumps(data)
print(json_string)
```
4. **Writing JSON to a File:**
- The `json.dump()` function is used to write JSON data to a file.
```python
import json
data = {'name': 'John Doe', 'age': 30, 'city': 'New York'}
with open('output.json', 'w') as file:
json.dump(data, file)
```
5. **Accessing JSON Data:**
- Once you've loaded JSON data, you can access individual elements using dictionary or list indexing.
```python
import json
json_string = '{"name": "John Doe", "age": 30, "city": "New York"}'
data = json.loads(json_string)
print(data['name']) # Output: John Doe
print(data['age']) # Output: 30
```
6. **Modifying JSON Data:**
- After loading JSON data, you can modify it like any other Python data structure.
```python
import json
json_string = '{"name": "John Doe", "age": 30, "city": "New York"}'
data = json.loads(json_string)
data['age'] = 31 # Modify the age
data['city'] = 'San Francisco' # Modify the city
# Convert the modified data back to JSON string
modified_json_string = json.dumps(data)
```
## More detailed Examples
#### Examples of how to parse and manipulate JSON data in Python, along with explanations and sample outputs.
### Example 1: Parsing JSON Data
```python
import json
# JSON data as a string
json_data = '{"name": "John Doe", "age": 30, "city": "New York"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing elements in the dictionary
print(data['name']) # Output: John Doe
print(data['age']) # Output: 30
```
**Explanation**: In this example, we have a JSON string `json_data` representing a person's information. We use `json.loads()` to parse this string into a Python dictionary called `data`. We then access individual elements within the dictionary using keys.
**Output**:
```
John Doe
30
```
### Example 2: Modifying JSON Data
```python
import json
# JSON data as a string
json_data = '{"name": "John Doe", "age": 30, "city": "New York"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Modify data
data['age'] = 31
data['city'] = 'San Francisco'
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data) # Output: {"name": "John Doe", "age": 31, "city": "San Francisco"}
```
**Explanation**: In this example, we first parse a JSON string into a dictionary. Then, we modify the `age` and `city` values. After the modifications, we convert the updated dictionary back to a JSON string using `json.dumps()`.
**Output**:
```
{"name": "John Doe", "age": 31, "city": "San Francisco"}
```
### Example 3: Adding a New Key-Value Pair
```python
import json
# JSON data as a string
json_data = '{"name": "John Doe", "age": 30, "city": "New York"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Add a new key-value pair
data['occupation'] = 'Engineer'
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data)
# Output: {"name": "John Doe", "age": 30, "city": "New York", "occupation": "Engineer"}
```
**Explanation**: Here, we parse a JSON string into a dictionary, then add a new key-value pair (`'occupation': 'Engineer'`). We then convert the updated dictionary back to a JSON string.
**Output**:
```
{"name": "John Doe", "age": 30, "city": "New York", "occupation": "Engineer"}
```
### Example 4: Removing a Key-Value Pair
```python
import json
# JSON data as a string
json_data = '{"name": "John Doe", "age": 30, "city": "New York"}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Remove a key-value pair
del data['age']
# Convert back to JSON
updated_json_data = json.dumps(data)
print(updated_json_data) # Output: {"name": "John Doe", "city": "New York"}
```
**Explanation**: In this example, we parse a JSON string into a dictionary and then remove the key `'age'`. We then convert the modified dictionary back to a JSON string.
**Output**:
```
{"name": "John Doe", "city": "New York"}
```
### Example 5: Working with Nested JSON
```python
import json
# JSON data as a string with nested structure
json_data = '{"person": {"name": "John Doe", "age": 30, "city": "New York"}}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing nested elements
print(data['person']['name']) # Output: John Doe
print(data['person']['age']) # Output: 30
```
**Explanation**: Here, we have a JSON string with a nested structure. We parse it into a dictionary and then access elements within the nested structure using multiple keys.
**Output**:
```
John Doe
30
```
### Example 6: Handling JSON Arrays
```python
import json
# JSON data as a string with an array
json_data = '{"fruits": ["apple", "banana", "cherry"]}'
# Parse JSON string to a Python dictionary
data = json.loads(json_data)
# Accessing elements in the array
print(data['fruits'][0]) # Output: apple
print(data['fruits'][2]) # Output: cherry
```
**Explanation**: In this example, we have a JSON string with an array of fruits. We parse it into a dictionary and then access elements in the array using indices.
**Output**:
```
apple
cherry
```
### Example 7: Working with JSON Objects in a List
```python
import json
# JSON data as a string with a list of objects
json_data = '[{"name": "John Doe", "age": 30}, {"name": "Jane Doe", "age": 25}]'
# Parse JSON string to a Python list of dictionaries
data = json.loads(json_data)
# Accessing elements in the list of objects
print(data[0]['name']) # Output: John Doe
print(data[1]['age']) # Output: 25
```
**Explanation**: Here, we have a JSON string with a list of objects representing people. We parse it into a list of dictionaries and then access elements within each dictionary.
**Output**:
```
John Doe
25
```
### Example 8: Handling JSON with Dates
```python
import json
from datetime import datetime
# Python dictionary with a date
data = {'date': datetime(2023, 11, 1).isoformat()}
# Convert to JSON
json_data = json.dumps(data)
print(json_data) # Output: {"date": "2023-11-01T00:00:00"}
```
**Explanation**: In this example, we have a Python dictionary with a date (represented as a `datetime` object). We convert it to a JSON string using `json.dumps()`.
**Output**:
```
{"date": "2023-11-01T00:00:00"}
```
### Example 9: Error Handling for Invalid JSON
```python
import json
# Invalid JSON data
invalid_json = '{"name": "John Doe", "age": 30, "city": "New York"'
try:
data = json.loads(invalid_json)
print(data)
except json.JSONDecodeError as e:
print(f"Error: {e}")
```
**Explanation**: This example demonstrates error handling for cases where the provided string is not valid JSON. The `json.loads()` function can raise a `JSONDecodeError`, which we catch and print.
**Output**:
```
Error: Expecting ',' delimiter: line 1 column 35 (char 34)
```
### Example 10: Reading and Writing from/to JSON Files
```python
import json
# Writing to a JSON file
data_to_write = {'name': 'John Doe', 'age': 30, 'city': 'New York'}
with open('output.json', 'w') as file:
json.dump(data_to_write, file)
# Reading from a JSON file
with open('output.json', 'r') as file:
data_read = json.load(file)
print(data_read) # Output: {'name': 'John Doe', 'age': 30, 'city': 'New York'}
```
**Explanation**: In this example, we first write a Python dictionary to a JSON file using `json.dump()`. Then, we read the contents of the JSON file back into a Python dictionary using `json.load()`.
**Output**: None (file created) / `{'name': 'John Doe', 'age': 30, 'city': 'New York'}` (printed content)
These examples cover a range of scenarios for parsing and manipulating JSON data in Python. They demonstrate common operations like reading from files, handling nested structures, and working with both dictionaries and lists in JSON format.