【问题标题】:How to generate json schema from python dict如何从 python dict 生成 json 模式
【发布时间】:2021-07-19 20:34:53
【问题描述】:

我正在寻找一个用于从 python 字典生成 json_schema 的 python 库

例如这本词典

fields = {
  "field1": {"title": "Field1 Title", "type": "string"},
  "field2": {"title": "Field2 Title", "type": "integer"},
  "field3": {"title": "Field3 Title", "type": "numeric", "description": "description", "multipleOf": 0.01},
}

应该处理到下一个json_schema:

{
    "title": "",
    "type": "object",
    "properties": {
        "field1": {
            "title": "Field1 Title",
            "type": "string"
        },
        "field2": {
            "title": "Field2 Title",
            "type": "integer"
        },
        "field3": {
            "title": "Field3 Title",   
            "description": "description",
            "type": "numeric",
            "multipleOf": 0.01,
        }
    },
    "required": ["field1", "field2"]
}

【问题讨论】:

  • Python 是一种动态语言,因此将第一个数据结构转换为第二个数据结构非常简单。
  • 您可能希望为此进行自定义键、值映射,因为在您的情况下它不是 1:1 映射。请参考此线程的答案:stackoverflow.com/questions/26745519/…

标签: python json jsonschema json-schema-validator python-jsonschema


【解决方案1】:

以 json 形式加载内容 -> json.loads(file)

【讨论】:

    猜你喜欢
    • 2021-12-05
    • 2019-11-24
    • 2016-05-15
    • 2020-09-27
    • 1970-01-01
    • 2017-02-22
    • 2017-05-24
    • 2019-06-23
    • 1970-01-01
    相关资源
    最近更新 更多