0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

jsonをTypedDictのクラスに変換してくれるサービス

Posted at

apiから帰ってくる内容をTypedDictにしたい!
でもめんどい

というときに使えるのが

左にjsonを入れるだけで右にTypedDictのクラスにしてくれるのだ!!!

例えばこれを入力すれば

{
  "emojis": [
    {
      "id": 1,
      "name": "thumbsup",
      "alias": ["+1"]
    },
    {
      "id": 2,
      "name": "thumbsdown",
      "alias": ["-1"]
    },
    {
      "id": 100,
      "name": "100"
    }
  ]
}

こんな感じにしてくれる

from typing import List, Union

from typing_extensions import TypedDict


class EmojisItem0(TypedDict):
    id: int
    name: str
    alias: List[str]


class EmojisItem2(TypedDict):
    id: int
    name: str


class Root(TypedDict):
    emojis: List[Union[EmojisItem0, EmojisItem2]]

この場合のaliasがあるのとないのをNotRequiredでやってくれないけど、それ以外はいい感じに変換してくれる

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?