Variable object names in JSON schema
json, jsonschema
Solution
You can use "patternProperties" instead of "properties":
"codenumber": {
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "array",
"items": {
"type": "object",
"properties": {
"prop1": { "type": "string" },
"prop2": { "type": "string" }
}
}
}
}
}
Problem
I'm attempting to define the following in JSON schema: ``` "codenumber": { "12345": [ { "prop1": "yes", "prop2": "no" } ] } ``` The codenumber object contains a property "12345" which is always a string number that contains an array. The number value can change however, so I cannot simply define this like so: ``` "codenumber": { "type": "object", "properties": { "12345": { "type": "array", "items": { "type": "object", "properties": { "prop1": { "type": "string" }, "prop2": { "type": "string" } } } } } } ``` Any way I can just define the first property name to be of any type of string?