【问题标题】:JSON Schema to validate an array of dictionaries用于验证字典数组的 JSON Schema
【发布时间】:2020-11-01 09:14:02
【问题描述】:

我最近向this 询问了有关如何删除列表中的名称的问题,但我意识到我真正想做的是删除数组中字典的名称。

我想验证这样的结构(注意字典没有命名):

{
    "some list": [
        {
            "foo": "bar"
        },
        {
            "bin": "baz"
        }
    ]
}

【问题讨论】:

  • 我不清楚你想如何约束你所描述的结构。你有一个对象数组。对象始终是属性名称到属性值的映射。如果您不想通过属性名称来识别值,如何识别它们?也就是说:您希望在一个地方看到一个字符串,在另一个地方看到一个整数。您将如何描述(用英语,而不是模式)您希望在哪里看到这些值?
  • 我只是希望能够验证上述结构。它是一个字典列表。这就是我真正关心的。该代码是我最好的猜测如何做到这一点,但我知道它不起作用。至于如何识别它们,我知道它们在哪里,这足以让我识别它们。我查找了一个列表,并知道里面有字典,它们不难找到或操作。我只需要知道如何使用模式来验证这样的结构。
  • 我想要类似的东西:一个列表包含任意数量的字典,所有字典都具有特定范围内的特定键和值。基本上就像任何其他对象一样,但是列表中的字典没有键名,如果是字典,则只有一个原始列表。如果这无法用模式表示,那么我将不得不提出自己的验证方案。
  • 你不能用“additionalProperties“: { „anyOf“: [{...}, {...}] }吗?或者干脆把它做成一个数组,然后把anyOf放到它的items中。
  • @carsten 我不知道,我对架构不是很熟悉。如果您知道可行的答案,请继续发布

标签: python json schema jsonschema


【解决方案1】:

我试过了。不要怀疑我的文件来自不同的问题。

[  {
"type": "Feature",
"geometry": {
  "type": "Point",
  "coordinates": [
    52.743356,
    -111.546907
  ]
},
"properties": {
  "dealerName": "Smiths Equipment Sales (Smiths Hauling)",
  "address": "HWY 13 - 5018 Alberta Avenue",
  "city": "Lougheed",
  "state": "AB",
  "zip": "T0B 2V0",
  "country": "Canada",
  "website": "http://smithsequipsales.com/",
  "phone": "780-386-3842",
  "dealerCode": "SMI06",
  "tractor": true,
  "utv": false,
  "hp": false
}  },  {
"type": "Feature",
"geometry": {
  "type": "Point",
  "coordinates": [36.16876,-84.07945]
},
"properties": {
  "dealerName": " Tommy's Motorsports",
  "address": "2401 N Charles G Seivers Blvd",
  "city": "Clinton",
  "state": "TN",
  "statename": "United States",
  "zip": "37716",
  "phone": "865-494-6500",
  "website": "https://www.tommysmotorsports.com/",
  "dealerCode": "TOM08",
  "tractor": true,
  "utv": false,
  "hp": false
  }
}
]

你可以这样输入:

import json
   data = json.load(open('test.json'))

    for i in data:
          print(i["type"])

【讨论】:

    【解决方案2】:

    问题似乎在于尝试将数组描述为“具有未命名属性的对象”时含糊不清。

    如果你在中间省略了不必要的对象,你最终会得到一个简单的模式:

    {
      "type": "object",
      "properties": {
        "some list": {
          "type": "array",
          "items": {
            "anyOf": [
              {
                "type": "string",
                "description": "a string"
              },
              {
                "type": "integer",
                "minimum": 0,
                "description": "Default time to expose a single image layer for."
              }
            ]
          }
        }
      }
    }
    

    【讨论】:

    【解决方案3】:

    JSON 模式替代方案(仅限类型检查):

    from dataclasses import asdict, dataclass
    from typing import List, Union
    
    from validated_dc import ValidatedDC
    
    
    @dataclass
    class Foo(ValidatedDC):
        foo: str
    
    
    @dataclass
    class Bin(ValidatedDC):
        bin: str
    
    
    @dataclass
    class Items(ValidatedDC):
        some_list: List[Union[Foo, Bin]]
    
    
    data = [
        {
            "foo": "bar"
        },
        {
            "bin": "baz"
        }
    ]
    
    items = Items(some_list=data)
    assert items.get_errors() is None
    assert isinstance(items.some_list[0], Foo)
    assert isinstance(items.some_list[1], Bin)
    assert asdict(items) == {'some_list': data}
    

    ValidatedDC - https://github.com/EvgeniyBurdin/validated_dc

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-24
      • 2020-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-04
      • 2014-04-29
      • 2018-11-22
      • 1970-01-01
      相关资源
      最近更新 更多