json-to-pydantic

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

JSON to Pydantic Skill

JSON 转 Pydantic 技能

This skill helps convert raw JSON data or API responses into structured, strongly-typed Python classes using Pydantic.
本技能可帮助你将原始JSON数据或API响应转换为结构化、强类型的Python类,使用Pydantic实现。

Instructions

操作步骤

  1. Analyze the Input: Look at the JSON object provided by the user.
  2. Infer Types:
    • string
      ->
      str
    • number
      ->
      int
      or
      float
    • boolean
      ->
      bool
    • array
      ->
      List[Type]
    • null
      ->
      Optional[Type]
    • Nested Objects -> Create a separate sub-class.
  3. Follow the Example: Review
    examples/
    to see how to structure the output code. notice how nested dictionaries like
    preferences
    are extracted into their own class.
    • Input:
      examples/input_data.json
    • Output:
      examples/output_model.py
  1. 分析输入:查看用户提供的JSON对象。
  2. 推断类型
    • string
      ->
      str
    • number
      ->
      int
      float
    • boolean
      ->
      bool
    • array
      ->
      List[Type]
    • null
      ->
      Optional[Type]
    • 嵌套对象 -> 创建独立的子类。
  3. 参考示例: 查看
    examples/
    目录下的内容,了解如何构建输出代码。注意像
    preferences
    这样的嵌套字典是如何提取为独立类的。
    • 输入:
      examples/input_data.json
    • 输出:
      examples/output_model.py

Style Guidelines

风格指南

  • Use
    PascalCase
    for class names.
  • Use type hints (
    List
    ,
    Optional
    ) from
    typing
    module.
  • If a field can be missing or null, default it to
    None
    .
  • 类名使用
    PascalCase
    命名法。
  • 使用
    typing
    模块中的类型提示(
    List
    Optional
    )。
  • 如果某个字段可能缺失或为null,默认值设为
    None