-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_JSON.py
More file actions
71 lines (48 loc) · 1.85 KB
/
Copy pathpython_JSON.py
File metadata and controls
71 lines (48 loc) · 1.85 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
# ============================================
# Task 1 — Create and Serialise a Nested JSON Object
# ============================================
import json
student = {
"name": "Ajay",
"age": 24,
"passed": True,
"marks": [88, 76, 95],
"address": {
"city": "Hyderabad",
"pincode": 500001
}
}
# Convert to formatted JSON string
student_json = json.dumps(student, indent=4)
print("Serialized JSON:\n", student_json)
print("\n============================================\n")
# ============================================
# Task 2 — Parse JSON and Access Values
# ============================================
parsed_data = json.loads(student_json)
print("Name:", parsed_data["name"])
print("Second Mark:", parsed_data["marks"][1])
print("City:", parsed_data["address"]["city"])
print("\n============================================\n")
# ============================================
# Task 3 — Identify and Fix Invalid JSON
# ============================================
# Errors in original JSON:
# 1. Keys must be in double quotes → name is not quoted
# 2. Trailing comma is not allowed after last key-value pair
bad_json = '{ name: "Raj", "score": 88, "active": true, }'
# Fixed JSON
fixed_json = '{ "name": "Raj", "score": 88, "active": true }'
parsed_fixed = json.loads(fixed_json)
print("Fixed JSON Parsed:", parsed_fixed)
print("\n============================================\n")
# ============================================
# Task 4 — Build a JSON Array of Objects
# ============================================
products = [
{"product_name": "Laptop", "price": 70000, "in_stock": True},
{"product_name": "Mouse", "price": 500, "in_stock": True},
{"product_name": "Keyboard", "price": 1500, "in_stock": False}
]
products_json = json.dumps(products, indent=2)
print("Products JSON:\n", products_json)