在Python中使用重复的Json响应验证Json模式

2024-10-03 09:07:05 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试使用来自Jsonschema.validate的validate来验证Json模式和Json响应时,我没有得到任何结果,而它在https://www.jsonschemavalidator.net/上显示matched

Json模式

 {
        "KPI": [{
            "KPIDefinition": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "version": {
                    "type": "number"
                },
                "description": {
                    "type": "string"
                },
                "datatype": {
                    "type": "string"
                },
                "units": {
                    "type": "string"
                }
            },
            "KPIGroups": [{
                "id": {
                    "type": "number"
                },
                "name": {
                    "type": "string"
                }
            }]
        }],
        "response": [{
            "Description": {
                "type": "string"
            }
        }]
    }

JSON Response JSON Response

{
  "KPI": [
    {
      "KPIDefinition": {
        "id": "2",
        "name": "KPI 2",
        "version": 1,
        "description": "This is KPI 2",
        "datatype": "1",
        "units": "perHour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    },
    {
      "KPIDefinition": {
        "id": "3",
        "name": "Parameter 3",
        "version": 1,
        "description": "This is KPI 3",
        "datatype": "1",
        "units": "per Hour"
      },
      "KPIGroups": [
        {
          "id": 7,
          "name": "Group 7"
        }
      ]
    }
  ],
  "response": [
    {
      "Description": "RECORD Found"
    }
  ]
}

Code

json_schema2 = {"KPI":[{"KPIDefinition":{"id_new":{"type":"number"},"name":{"type":"string"},"version":{"type":"number"},"description":{"type":"string"},"datatype":{"type":"string"},"units":{"type":"string"}},"KPIGroups":[{"id":{"type":"number"},"name":{"type":"string"}}]}],"response":[{"Description":{"type":"string"}}]}

json_resp = {"KPI":[{"KPIDefinition":{"id":"2","name":"Parameter 2","version":1,"description":"This is parameter 2 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]},{"KPIDefinition":{"id":"3","name":"Parameter 3","version":1,"description":"This is parameter 3 definition version 1","datatype":"1","units":"kN"},"KPIGroups":[{"id":7,"name":"Group 7"}]}],"response":[{"Description":"RECORD FETCHED"}]}

print(jsonschema.validate(instance=json_resp, schema=json_schema2))

验证没有正确完成,我在响应中更改了数据类型和键名,但仍然没有引发异常或错误


Tags: nameidnumberstringversionresponsetypedescription
1条回答
网友
1楼 · 发布于 2024-10-03 09:07:05

任何事都可以

您的模式对象和JSON对象都是正常的,如果它没有引发任何异常(这里似乎是这样),那么验证已经通过

也就是说,您应该将调用包装在try-except块中,以便能够捕获验证错误

比如:

try:
    jsonschema.validate(...)
    print("Validation passed!")
except ValidationError:
    print("Validation failed")
# similarly catch SchemaError too if needed.

更新:您的架构无效。目前,它将验证几乎所有的输入。模式JSON应该是一个对象(dict),它应该有“type”这样的字段,并且根据类型,它可能有其他必需的字段,如“items”或“properties”。请仔细阅读如何编写JSONSchema

以下是我为您的JSON编写的模式:

{
  "type": "object",
  "required": [
    "KPI",
    "response"
  ],
  "properties": {
    "KPI": {
        "type": "array",
        "items": {
          "type": "object",
            "required": ["KPIDefinition","KPIGroups"],
            "properties": {
              "KPIDefinition": {
                "type": "object",
                "required": ["id","name"],
                "properties": {
                  "id": {"type": "string"},
                  "name": {"type": "string"},
                  "version": {"type": "integer"},
                  "description": {"type": "string"},
                  "datatype": {"type": "string"},
                  "units": {"type": "string"},
                },
                "KPIGroups": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": ["id", "name"],
                    "properties": {
                      "id": {"type": "integer"},
                      "name": {"type": "string"}
                    }
                  }
                }
              }
            }
        }
    },
    "response": {
        "type": "array",
        "items": {
            "type": "object",
            "required": ["Description"],
            "properties": {
              "Description": {"type": "string"}
            }
        }
    }
  }
}

相关问题 更多 >