简体   繁体   中英

JSON Schema to validate an array of dictionaries

I recently asked this question about how to get rid of a name on a list, but I realized what I really want to do is get rid of the names of the dictionaries in the array.

I want to validate a structure like this (notice the dictionaries are not named):

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

The issue seems to be the ambiguity of trying to describe an array as "object with unnamed properties".

If you leave out the unnecessary object in between, you end up with a straightforward schema:

{
  "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."
          }
        ]
      }
    }
  }
}

I tried this.Don't wonder I had the file from a different problem.

[  {
"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
  }
}
]

You can enter it like that:

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

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

JSON schema alternative (type check only):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM