PY
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
from dataclasses import dataclass
import json
@dataclass
class Product:
# All elements declared
name: str
price: float
category: str
description: str
# Sample JSON string
json_string = """
{
"name": "Mac M1 Laptop",
"price": 1999.99,
"category": "Electronics",
"description": "A powerful laptop for work and play"
}
"""
# Deserialize the JSON string into a Product object
product = json.loads(json_string, object_hook=lambda d: Product(**d))
# Accessing the deserialized data
print(product.name) # Output: Laptop
print(product.price) # Output: 999.99
print(product.description) # Output: A powerful laptop for work and play
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run