You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求推荐可原生处理JSON Schema的Python对象管理模块(非验证工具)

寻找Python的JSON Schema对象管理模块

你想要的是一款能原生识别JSON Schema核心概念(比如dependencies、definitions)、专门用来管理Schema结构而非验证数据的Python模块对吧?刚好有几个工具能完美满足你的需求,比json+dictor的组合更贴合Schema场景:

推荐工具及实用示例

1. jsonschema——不要只把它当验证工具

虽然jsonschema最出名的是数据验证功能,但它内置了完整的JSON Schema结构解析逻辑,能原生处理所有Schema关键字,完全可以单独用来管理Schema对象。

下面是适配你现有代码逻辑的示例:

from jsonschema import Draft202012Validator  # 选对应你的Schema版本的Validator
from pathlib import Path
import json

def schema_lister(device, path):
    path_conf = Path(__file__).resolve().parent.parent
    schema_dir = f"_static/tmp/{device}_schema.json"
    path_schema = Path(path_conf, schema_dir)
    
    with open(path_schema) as json_file:
        schema_json = json.load(json_file)
    
    # 用Validator加载Schema,它会自动处理Schema的内部结构
    validator = Draft202012Validator(schema_json)
    # 直接访问Schema的原生属性,比如definitions、dependencies
    if "definitions" in validator.schema:
        print(f"已找到定义项:{list(validator.schema['definitions'].keys())}")
    
    # 实现类似dictor的路径访问功能
    target_node = validator.schema
    for part in path.split("."):
        if part in target_node:
            target_node = target_node[part]
        else:
            target_node = None
            break
    
    if target_node and isinstance(target_node, dict):
        for elm, elm_schema in target_node.items():
            if elm not in ["anyOf", "dependencies"] and isinstance(elm_schema, dict):
                # 这里写你的业务逻辑,比如打印标题、描述
                print(f"元素:{elm}")
                print(f"标题:{elm_schema.get('title', '无标题')}")
                print(f"描述:{elm_schema.get('description', '无描述')}\n")

2. json-schema-ref-parser——专注Schema引用与结构处理

如果你经常需要处理Schema中的$ref引用(比如引用definitions里的内容),这个工具会更合适——它能自动解析所有引用,把分散的Schema片段整合起来,同时保留原结构,非常适合深度管理Schema。

示例代码:

from json_schema_ref_parser import RefParser
from pathlib import Path

def schema_lister(device, path):
    path_conf = Path(__file__).resolve().parent.parent
    schema_dir = f"_static/tmp/{device}_schema.json"
    path_schema = Path(path_conf, schema_dir)
    
    # 加载并自动解析所有$ref引用和definitions
    parser = RefParser()
    schema = parser.parse(path_schema.as_uri())
    
    # 实现路径访问逻辑
    def traverse_schema(schema, path_parts):
        current = schema
        for part in path_parts:
            if part in current:
                current = current[part]
            else:
                return None
        return current
    
    target_node = traverse_schema(schema, path.split("."))
    if target_node and isinstance(target_node, dict):
        for elm, elm_schema in target_node.items():
            if elm not in ["anyOf", "dependencies"] and isinstance(elm_schema, dict):
                # 执行你的业务逻辑
                print(f"元素名:{elm}")
                print(f"说明:{elm_schema.get('description', '无说明')}\n")

为什么选这些工具?

  • 它们都原生理解JSON Schema规范,不需要你手动处理dependencies、definitions这类关键字的特殊逻辑;
  • 相比json+dictor,能更安全、高效地处理Schema的嵌套和引用,减少冗余代码;
  • 完全可以只用来管理Schema结构,不需要启用任何验证功能,完美匹配你的需求。

内容的提问来源于stack exchange,提问作者mfcss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:17:35