JSON to Python
Convert JSON data to Python dict, dataclass, or TypedDict syntax.
์์ฃผ ๋ฌป๋ ์ง๋ฌธ
์ฝ๋ ๊ตฌํ
import json
json_str = '{"name": "Alice", "age": 30, "active": true}'
data = json.loads(json_str)
print(data) # {'name': 'Alice', 'age': 30, 'active': True}
print(data["name"]) # Alice
# Using dataclass
from dataclasses import dataclass
from typing import Optional
@dataclass
class Person:
name: str
age: int
active: bool
person = Person(**data)
print(person.name) # Alice
# Using TypedDict
from typing import TypedDict
class PersonDict(TypedDict):
name: str
age: int
active: bool
typed: PersonDict = data
print(typed["age"]) # 30Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.