如何要求JSON列表中的数据?

如何要求列表中的数据?如何做到这一点并指定类型,例如字典?

以下是两个JSON示例和一个模式。根据架构,这两个JSON示例均有效。列表为空的样本应通过IMHO验证。我如何做到这一点?


from jsonschema import validate


# this is ok per the schema
{
    "mylist":[
      {
        "num_items":8,
        "freq":8.5,
        "other":2
      },
      {
        "num_items":8,
        "freq":8.5,
        "other":4
      }
    ]
}

# this should fail validation, but does not.
{
    "mylist":[

    ]
}

# schema
{
  "$schema": "http://json-schema.org/schema#",
  "type": "object",
  "properties": {
    "mylist": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "num_items": {
            "type": "integer"
          },
          "freq": {
            "type": "number"
          },
          "other": {
            "type": "integer"
          }
        },
        "required": [
          "freq",
          "num_items",
          "other"
        ]
      }
    }
  },
  "required": [
    "mylist"
  ]
}


评论