【问题标题】:how to validate json schema of the given json structure如何验证给定 json 结构的 json 模式
【发布时间】:2021-09-05 21:30:42
【问题描述】:

你好,我的json结构如下:

{
"datasetType": "monolingual-corpus",
"languages": {
    "sourceLanguage": "hi"
},
"collectionSource": [
    "http://pib.gov.in/"
],
"domain":[
    "news"
],
"license": "cc-by-4.0",
"submitter": {
    "name": "Project aroad",
    "aboutMe": "Open source project run by aroad foundation",
    "team": [
        {
            "name": "Navneet Kumar hegde",
            "aboutMe": "NLP team lead at Project aroad"
        },
        {
            "name": "Aswini Pradeep",
            "aboutMe": "Backend team lead at Project aroad"
        }
    ]
}

我只能使用 json 模式来验证 datasetType。我如何验证其他值,例如“languages”、“collectoinsource”、“submitter”。 在“提交者”中,所有字段都应为必填项,以及如何在“提交者”中验证“团队”

我用 python 编写的代码仅验证“datasetType”,无法验证剩余字段。 请帮助我,提前谢谢

【问题讨论】:

    标签: python json python-3.x jsonschema


    【解决方案1】:

    试试marshmallow。它非常适合验证模式。

    from marshmallow import Schema, fields
    
    
    class LanguageSchema(Schema):
        sourceLanguage = fields.String(required=True)
    
    
    class UserSchema(Schema):
        name = fields.String(required=True)
        aboutMe = fields.String(required=True)
    
    
    class SubmitterSchema(UserSchema):
        team = fields.List(fields.Nested(UserSchema()))
    
    
    class ExampleSchema(Schema):
        datasetType = fields.String(required=True)
        languages = fields.Nested(LanguageSchema(), required=True)
        collectionSource = fields.List(fields.URL, required=True)
        domain = fields.List(fields.String(), required=True)
        license = fields.String(required=True)
        submitter = fields.Nested(SubmitterSchema(), required=True)
    
    
    data = {
        "datasetType": "monolingual-corpus",
        "languages": {
            "sourceLanguage": "hi"
        },
        "collectionSource": [
            "http://pib.gov.in/"
        ],
        "domain": [
            "news"
        ],
        "license": "cc-by-4.0",
        "submitter": {
            "name": "Project aroad",
            "aboutMe": "Open source project run by aroad foundation",
            "team": [
                {
                    "name": "Navneet Kumar hegde",
                    "aboutMe": "NLP team lead at Project aroad"
                },
                {
                    "name": "Aswini Pradeep",
                    "aboutMe": "Backend team lead at Project aroad"
                }
            ]
        }
    }
    
    # initialize schema
    schema = ExampleSchema()
    # validate data, will throw error if data does not fit schema
    validated_data = schema.load(data)
    
    

    【讨论】:

      【解决方案2】:

      您可以使用一个名为 JSON Schema 的标准和一个 Python 库 jsonschema 来使用您自己的架构验证 json 数据。 https://python-jsonschema.readthedocs.io/en/stable/validate/

      至于编写架构和验证您想要的内容,您有几种方法可以解决。

      1. 要强制执行数据结构,您只需使用基本对象和数组规范。 https://json-schema.org/learn/miscellaneous-examples.html
      2. 要强制使用允许的值(即在语言中),您可以使用 enum 属性。 https://stackoverflow.com/a/30931659/4739483

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2017-08-16
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多