如何在烧瓶中针对jsonschema验证json?

时间:2019-11-18 13:49:46

标签: python json flask

任何python库是否都支持功能,我们可以在其中传递一个参数jsonschema,第二个参数json并对其进行验证并返回True或False?我对jsonschema的控制方式没有任何控制权吗?

示例:

json:
{
  "id":1,
  "name":"intel"
}

使用(https://www.liquid-technologies.com/online-json-to-schema-converter)生成jsonschema。

jsonschema:
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    }
  },
  "required": [
    "id",
    "name"
  ]
}

如果我将使用https://jsonschema.net/生成jsonschema,则

jsonschema:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
 "id",
 "name"
 ],
"properties": {
        "id": {
       "$id": "#/properties/id",
       "type": "integer",
       "title": "The Id Schema",
       "default": 0,
       "examples": [
         1
       ]
     },
     "name": {
       "$id": "#/properties/name",
       "type": "string",
       "title": "The Name Schema",
       "default": "",
       "examples": [
        "intel"
       ],
       "pattern": "^(.*)$"
     }
   }
}

因此,用户可以使用任何网站来生成jsonschema并通过post api存储到db中。 还有另一个post api,其中输入json将针对用户存储的jsonschema进行验证。 如何解决这种情况? 提前致谢 !

1 个答案:

答案 0 :(得分:0)

我知道如何解决它。我正在使用jsonschema库进行验证。 从jsonschema导入验证 使用这个validate()方法将获取json和jsonschema并对其进行验证。