PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import json
from dataclasses import make_dataclass
json_str = '{"name": "Mac M1 laptop", "price": 1999.99, "category": "Electronics", "description": "A great device for work and pleasure"}'
data = json.loads(json_str)
required_fields = ["name", "price"] # "description"]
Product = make_dataclass("Product", [(field, type(data[field])) for field in required_fields])
product = Product(**{field: data[field] for field in required_fields})
print(product.name) # Output: Mac M1
print(product.price) # Output: 1999.99
#print(product.description) # Output: A great device for work and pleasure
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run