JSON 验证字典数组的模式
JSON Schema to validate an array of dictionaries
我最近问 关于如何删除列表中的名称的问题,但我意识到我真正想做的是删除数组中字典的名称。
我想验证这样的结构(注意字典没有命名):
{
"some list": [
{
"foo": "bar"
},
{
"bin": "baz"
}
]
}
我试过了this.Don难怪我有来自不同问题的文件。
[ {
"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"])
问题似乎是试图将数组描述为“具有未命名属性的对象”的歧义。
如果省略中间不必要的对象,您将得到一个简单的模式:
{
"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."
}
]
}
}
}
}
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}
我最近问
我想验证这样的结构(注意字典没有命名):
{
"some list": [
{
"foo": "bar"
},
{
"bin": "baz"
}
]
}
我试过了this.Don难怪我有来自不同问题的文件。
[ {
"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"])
问题似乎是试图将数组描述为“具有未命名属性的对象”的歧义。
如果省略中间不必要的对象,您将得到一个简单的模式:
{
"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."
}
]
}
}
}
}
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}