Created
March 29, 2021 10:43
-
-
Save MartinThoma/64e15168c9e058f7e25f490b58592513 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Core Library modules | |
import json | |
from typing import List | |
# Third party modules | |
import pydantic.json | |
from pydantic import BaseModel, parse_obj_as | |
class User(BaseModel): | |
name: str | |
age: int | |
# Deserialize a JSON string | |
users_str = '[{"name": "user1", "age": 15}, {"name": "user2", "age": 28}]' | |
users = parse_obj_as(List[User], json.loads(users_str)) | |
# Proof it! | |
print(users) | |
# Serialize | |
print(json.dumps([user.dict() for user in users])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment