You have defined your schema correctly, except that it doesn't match the data you say you are validating. If you change the property names to match the schema, you still have one issue. If you want to allow "toll" and "message" to be null, you can do the following.
{
"type": "array",
"items": {
"type": "object",
"properties": {
"loc": {
"type": "string"
},
"toll": {
"type": ["string", "null"]
},
"message": {
"type": ["string", "null"]
}
},
"required": [
"loc"
]
}
}
However, that isn't related to the error message you are getting. That message means that data you are validating is not an array. The example data you posted should not result in this error. Are you running the validator on some data other than what is posted in the question?
Answer from Jason Desrosiers on Stack OverflowVideos
You can achieve this using the anyOf keyword and definitions/$ref to avoid duplication.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"assetMetadata": {
"anyOf": [
{ "$ref": "#/definitions/assetMetaData" },
{
"type": "array",
"description": "...",
"items": { "$ref": "#/definitions/assetMetaData" }
}
]
}
},
"definitions": {
"assetMetadata": {
"type": "object",
"additionalProperties": false,
"properties": { ... }
}
}
}
The accepted answer was not working for me in the JSON schema validator.
The arrays were not being accepted.
I made some tweaks and changes to make it work, here is an example schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"anyOf": [
{
"$ref": "#/definitions/commentObject"
},
{
"type": "array",
"description": "Array of Comment objects",
"items": {
"$ref": "#/definitions/commentObject"
}
}
],
"definitions": {
"commentObject": {
"properties": {
"number": {
"type": "integer",
"minLength": 0,
"maxLength": 256
},
"comment": {
"type": "string",
"minLength": 0,
"maxLength": 256
}
},
"required": [
"number",
"comment"
],
"type": "object"
}
}
}
Object used to test the validation:
{
"number": 47,
"comment": "This is a comment",
}
Arrays of objects used to test the validation:
[
{
"number": 47,
"comment": "This is a comment"
},
{
"number": 11,
"comment": "This is other comment"
}
]
JSON Schema Validator for object
JSON Schema Validator for array (of the same objects)