JSON Schema with unknown property names
I want to have a JSON Schema with unknown property names in an array of objects. A good example is the meta-data of a web page:
"meta": {
"type": "array",
"items": {
"type": "object",
"properties": {
"unknown-attribute-1": {
"type": "string"
},
"unknown-attribute-2": {
"type": "string"
},
...
}
}
}
Any ideas please, or other way to reach the same?
Use patternProperties
instead of properties
. In the example below, the pattern match regex .*
accepts any property name and I am allowing types of string
or null
only by using "additionalProperties": false
.
"patternProperties": {
"^.*$": {
"anyOf": [
{"type": "string"},
{"type": "null"}
]
}
},
"additionalProperties": false
... or if you just want to allow a string in your "object" (like in the original question):
"patternProperties": {
"^.*$": {
{"type": "string"},
}
},
"additionalProperties": false
You can make constraints on properties not explicitly defined. The following schema enforces "meta" to be an array of objects whose properties are of type string:
{
"properties" : {
"meta" : {
"type" : "array",
"items" : {
"type" : "object",
"additionalProperties" : {
"type" : "string"
}
}
}
}
}
In case you just want to have an array of strings, you may use the following schema:
{
"properties" : {
"meta" : {
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}