pythonjsonschemajsonschema

JSON Schema for objects without names


I want to write part of a schema that validates a list of objects, where the objects in the list don't have a name:

"some list": [
    {
        "thing 1": "foo",
        "thing 2": 100
    },
    {
        "thing 1": "foo",
        "thing 2": 100
    },
    {
        "thing 1": "foo",
        "thing 2": 100
    },
]

I have a working schema that has the extra key name that I want to get rid of, labelled I WANT TO GET RID OF THIS NAME. I guess you could think of it as not having a property name for that object.

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "id": "v2",
    "properties": {
        "some list": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "I WANT TO GET RID OF THIS NAME": {
                        "type": "object",
                        "properties": {
                            "thing 1": {
                                "type": "string",
                                "description": "a string"
                            },
                            "thing 2": {
                                "type": "integer",
                                "minimum": 0,
                                "description": "Default time to expose a single image layer for."
                            }
                        },
                        "additionalProperties": false
                    },
                    "additionalProperties": false
                }
            }
        }
    }
}

I can't just git rid of the name because the schema spec expects it, but I also can't figure out how to tell it those objects don't have a name. I am running this example with Python 3.7 using jsonschema Draft7Validator


Solution

  • You are correct to get rid of that property - it is referring to the wrong level of nesting. The schema should look like:

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "id": "v2",
      "properties": {
        "some list": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "thing 1": {
                "type": "string",
                "description": "a string"
              },
              "thing 2": {
                "type": "integer",
                "minimum": 0,
                "description": "Default time to expose a single image layer for."
              }
            },
            "additionalProperties": false
          }
        }
      }
    }