Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

python - 如何在烧瓶中针对jsonschema验证json?(How to Validate json against jsonschema in flask?)

Is there any functionality support by any python library where we can pass one argument is jsonschema and second is json and validate it and return True or False?

(python库是否有任何功能支持,我们可以在其中传递一个参数jsonschema,第二个参数json并对其进行验证并返回True或False?)

I dont have any control on how the jsonschema will be?

(我对jsonschema的控制方式没有任何控制权?)

example:

(例:)

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

using ( https://www.liquid-technologies.com/online-json-to-schema-converter ) this to generate jsonschema.

(使用( 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"
  ]
}

if i will use https://jsonschema.net/ to generate jsonschema then

(如果我将使用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": "^(.*)$"
     }
   }
}

So the user can take any website to generate jsonschema and store into db through post api.

(因此,用户可以使用任何网站来生成jsonschema并通过post api存储到db中。)

And there is another post api where input json will validate against jsonschema which is stored by user.

(还有另一个post api,其中输入json将针对用户存储的jsonschema进行验证。)

How to solve this scenarios ?

(如何解决这种情况?)

Thanks in advance !

(提前致谢 !)

  ask by VVK kumar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I got how to work around it.

(我知道如何解决它。)

Am using jsonschema library for validating .

(我正在使用jsonschema库进行验证。)

from jsonschema import validate using this validate() which will take json and jsonschema and validate them.

(从jsonschema导入使用此validate()进行验证,它将接受json和jsonschema并对其进行验证。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...