2

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"
        }
    ]
}
7
  • it's not clear to me how you want to constrain the structure you are describing. you have an array of objects. objects are always a map of property names to property values. if you don't want to identify the values by their property name, how can they be identified? that is: you expect to see a string in one place, and an integer in another. how would you describe (in english, not schema) where you expect to see these values? Commented Jul 11, 2020 at 21:41
  • I just want to be able to validate a structure like the one above. It is a list of dictionaries. That's really all I care about. The code was my best guess how to do this, but I know it doesn't work. As to how they can be identified, I know where they are so that's enough for me to identify them. I look for a list and know there are dictionaries inside, they aren't hard to find or operate on. I just need to know how to massage a schema into validating a structure like this. Commented Jul 12, 2020 at 8:45
  • I want something like: A list contains any number of dictionaries, all with certain keys and values in a certain range. Basically just like any other object, but the dictionaries in the list don't have a key name, just a raw list if dictionaries. If this isn't possible to represent with schema, then I'll have to come up with my own validation scheme. Commented Jul 12, 2020 at 8:53
  • Couldn’t you use “additionalProperties“: { „anyOf“: [{...}, {...}] }? Or simply make it an array and put the anyOf in it‘s items. Commented Jul 12, 2020 at 15:25
  • @carsten I don't know, I'm not super familiar with schema. If you know of an answer that will work, go ahead and post it Commented Jul 12, 2020 at 20:18

3 Answers 3

2

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."
          }
        ]
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I needed. I was unaware of anyOf
Going through json-schema.org/understanding-json-schema is a good way of learning the basic aspects of JSON Schema.
0

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

Comments

0

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"])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.