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 Overflow
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › array
JSON Schema - array
For example, you may represent a street address such as 1600 Pennsylvania Avenue NW as a 4-tuple of the form: ... To do this, we use the prefixItems keyword. prefixItems is an array, where each item is a schema that corresponds to each index of the document's array.
Discussions

List of objects with required values
How do you define required values in a list? I have a list of objects with a property which is an enum with 10 options. But the list must contain at least 4 items with specific field values. For ex... More on github.com
🌐 github.com
1
1
June 30, 2022
Contains not working as expected with array of objects
JSON Schema is constraints based, and so anything you don't prohibit is allowed and considered valid. You haven't constrained additional properties using the additionalProperties keyword, so any key in the elements array objects are considered valid. More on github.com
🌐 github.com
4
1
jsonschema - How can a json schema represent an array with different objects? - Stack Overflow
Let me paraphrase to see if I understand correctly: a json array can natively have any combination of objects. But by adding an object description to the schema, I'm just explicitly stating that the array may have an object of that type, but that does not mean that the array must have an object ... More on stackoverflow.com
🌐 stackoverflow.com
JSON Schema for a list of objects that must contain one of three default props
Hello, I need help... How can I prepare JSON Schema for a list of objects that must contain one of three default props (1,77,90), e.g: More on github.com
🌐 github.com
4
1
January 3, 2022
🌐
Json-schema
tour.json-schema.org › content › 01-Getting-Started › 06-Array-of-Objects
Array of Objects: Getting Started | A Tour of JSON Schema
Learn how to define an array of objects with properties name and level, and how to set the items keyword in JSON schema.
🌐
JSON Schema
json-schema.org › learn › miscellaneous-examples
JSON Schema - Miscellaneous Examples
The schema below represents a complex object with various properties including name, age, address, and hobbies. The address property is an object with nested properties, and the hobbies property is an array of strings.
🌐
Cswr
cswr.github.io › JsonSchema › spec › arrays
Arrays - JSON Schema
Here we show how to specify collections of JSON types using possibly nested JSON Schemas. Arrays are used to represent ordered sets of values, such as the following sequence of strings:
🌐
Opis
opis.io › json-schema › 2.x › array.html
Array type | Opis JSON Schema
An array is valid against this keyword if all unchecked items are valid against the schema defined by the keyword value. An item is considered unchecked if items keyword or prefixItems keyword (starting with draft 2020-12) contains an array of schemas and doesn’t have a corresponding position (index). The value of the keyword must be a valid json schema (object, boolean).
Find elsewhere
🌐
JSON Schema
json-schema.org › understanding-json-schema › reference › object
JSON Schema - object
The required keyword takes an array of zero or more strings. Each of these strings must be unique. ... In Draft 4, required must contain at least one string. In the following example schema defining a user record, we require that each user has a name and e-mail address, but we don't mind if they don't provide their address or telephone number: ... { "type": "object", "properties": { "name": { "type": "string" }, "email": { "type": "string" }, "address": { "type": "string" }, "telephone": { "type": "string" } }, "required": ["name", "email"]}
🌐
Rjsf-team
rjsf-team.github.io › json schema › arrays
Arrays | react-jsonschema-form
import { RJSFSchema } from '@rjsf/utils'; import validator from '@rjsf/validator-ajv8'; const schema: RJSFSchema = { type: 'array', items: { type: 'string', }, additionalItems: { type: 'boolean', }, }; render(<Form schema={schema} validator={validator} />, document.getElementById('app')); Any of these options can be set globally if they are contained within the ui:globalOptions block. They can also be overridden on a per-field basis inside a ui:options block as shown below. Array items are orderable by default, and react-jsonschema-form renders move up/down buttons alongside them.
🌐
Json-schema
tour.json-schema.org › content › 01-Getting-Started › 05-Arrays
Arrays: Getting Started | A Tour of JSON Schema
1{ 2 "name": "John Doe", 3 "age": 25, 4 "hobbies": ["reading", "writing"] 5} In JSON Schema, defining an array is similar to defining an object. Use the type keyword with the value array to define an array.
🌐
GitHub
github.com › orgs › json-schema-org › discussions › 145
Contains not working as expected with array of objects · json-schema-org · Discussion #145
{ "$schema": "http://json-schema.org/draft-07/schema", "properties": { "elements": { "type": "array", "items": { "type": "object" }, "contains": { "properties": { "somePropertyA": { "type": "string", "enum": [ "true" ] } } } } }, "required":["elements"], }
🌐
Ajv
ajv.js.org › json-schema.html
JSON Schema | Ajv JSON schema validator
The value of the keyword should be an array of unique strings. The data object to be valid should contain all properties with names equal to the elements in the keyword value. ... The value of the keyword should be a map with keys equal to data ...
🌐
GeeksforGeeks
geeksforgeeks.org › json-schema
JSON Schema | GeeksforGeeks
June 22, 2020 - JSON Schema is a content specification language used for validating the structure of a JSON data.It helps you specify the objects and what values are valid inside the object's properties.
Top answer
1 of 2
2

Does the second schema correctly identify that "arr is an array of objects that have an integer named type an integer, and either an object named Steps or an object named Heartrate"?

Yes, somewhat. You do not state which properties must exist, and which properties cannot exist together at the same time, which we can do with the "required" and "oneOf" keywords.

Here's an updated schema, and also fixing an error you made in the "Heartrate" section (you said "steps" instead of "heartrates"):

Copy{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "arr": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [ "type" ],
        "oneOf": [
           { "required": ["Steps"] },
           { "required": ["HeartRate"] }
        },
        "properties": {
          "type": {
            "type": "integer"
          },
          "Steps": {
            "type": "object",
            "required": ["steps"],
            "properties": {
              "steps": {
                "type": "integer"
              }
            }
          },
          "HeartRate": {
            "type": "object",
            "required": ["heartrates"],
            "properties": {
              "heartrates": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
  }
}

2 of 2
1

Well, it passes because technically it is valid, you can have any combination of objects in an array. This schema explicitly states that there can be a heartrate object. You say you're familiar with arrays of a single object type, but what you should know is an array doesn't care what types of objects are in it, which is why your introduced schema is in fact valid. Whether or not it fits the function you're trying to achieve is another question all together.

It would imply that you would need either one very complex loop to traverse the array when you need to pull an object, assuming there will be so many that you can't adequately index them, or you would need some sort of index with address to the objects in the array, which might be cumbersome to write but possible. So the question is, what are you intending to do with these objects? Seems obvious from the properties you're using, seems like a fitness monitor of some sort, but then what your intention to do with the data isn't apparent so it's difficult to say what you could do even if this schema is valid, which keep in mind that it is indeed a valid structure for an array.

Although if you do have two types of objects, one could ask why not simply have an array for each type which will have supporting data pulling functions specific to their contents. But again it asks what you're intending to do.

Hope this helps your thought process.

UPDATE...

Not knowing exactly what you are intending to do, here is what I suggest you use as your data-structure format.

Copylet someArray = [
  {
    Steps: {
      steps: 3500
    },
    HeartRate: {
      heartrates: 4000
    }
  },
  {
    Steps: {
      steps: 3500
    },
    HeartRate: {
      heartrates: 4000
    }
  }
]

You examples have some redundancy in them it seems, where they are explicitly stating that something is an object and something is a property of that object. JavaScript already knows all that so no need to add more complexity to the functions you create to work with these that will then have to dig through all those layers. I suggest that you create one object and each has a property of the type you've declared that is an object and that object can if you want be expanded or condensed as you desire. This will mitigate a lot of complexity to your structure. And so you'll see that you have an Array of Objects and those objects contain properties representing the data you're intending to gather.

But this assumes that the data you're gathering is from multiple sources. Again would need to know a bit more about what you're trying to accomplish.

🌐
Reddit
reddit.com › r/learnjavascript › json schema: how to require a value to be one of another properties array values
r/learnjavascript on Reddit: JSON Schema: How to require a value to be one of another properties array values
May 2, 2024 -

So, I'm struggling to figure out how to word this, so apologies if this is confusing/unclear.

I have a (simplified) schema like so:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "fields/checkbox-multiple.json",
    "title": "...",
    "description": "...",
    "type": "object",
    "required": ["choices"],
    "properties": {
        "default": {
            "description": "Default value of the field (optional)"
        },
        "choices": {
            "description": "Choices for any type which has a multiple settings e.g. checkbox, radio, select etc",
            "type": "array",
            "minItems": 1,
            "items": {
                "required": ["value", "text"],
                "properties": {
                    "value": {
                        "description": "Value to be used in the field. This will be the setting value e.g. true, false for a radio field"
                    },
                    "text": {
                        "description": "Text displayed on the field in the Customizer",
                        "type": "string"
                    }
                }
            }
        }
    }
}

So the schema essentially creates a HTML Select field.

choices.value is the value of option tag, choices.text is the displayed text, fairly straight forward.
The default, is simply what the value of the input should be by default (again, sorry for stating the obvious).

Here's some React psuedo-code to make this clearer:

<select name="...">
  {props.choices.map((choice) => {
    return (props.default === props.value)
      ? <option value={choice.value} selected>{choice.text}</option>
      : <option value={choices.value}>{choice.text}</option>
  });
</select>

Get to the question already!

So you may have already gotten it...The value of default MUST BE one of the choices.value ...errr...values.

Something like this in my head, but Googling is giving me everything but what I'm looking for...

{
    "default": {
        "enum": {
            "oneOf": #choices.values.items.value
        }
    }
}

Also, default is not required, but if it is given it has to be one of the choices.

Hope that makes sense! Thank in advance

🌐
QA Touch
qatouch.com › home › validating json schema: all you need to know
Validating JSON Schema: All You Need To Know
June 24, 2025 - For illustration, the following schema demands that a value be an object with a minimum of two properties and a maximum of five properties: ... Dependencies between object properties are specified using the dependencies keyword. It requires an object with schemas or arrays of property names as values and property names as keys.
🌐
MongoDB
mongodb.com › working with data
jsonSchema working only for the first item of an array of objects - Working with Data - MongoDB Community Hub
September 10, 2023 - I had built an API that was working with NodeJS and express, ones I felt confident with it I learnt Mongo in order to get closer to the MERN stack. Everything is working smoothly and now, just for learning purposes, I’m trying to enforce a schema so that post and put methods are somewhat limited.
Top answer
1 of 4
28

I got it to work using this validator by nesting the part of the schema for the array elements inside a object with the name items. The schema now has two nested items fields, but that is because one is a keyword in JSONSchema and the other because your JSON actually has a field called items

JSONSchema:

{
   "type":"object",
   "properties":{
      "items":{
         "type":"array",
         "items":{
            "properties":{
               "item_id":{
                  "type":"number"
               },
               "quantity":{
                  "type":"number"
               },
               "price":{
                  "type":"number"
               },
               "title":{
                  "type":"string"
               },
               "description":{
                  "type":"string"
               }
            },
            "required":[
               "item_id",
               "quantity",
               "price",
               "title",
               "description"
            ],
            "additionalProperties":false
         }
      }
   }
}

JSON:

{
   "items":[
      {
         "item_id":1,
         "quantity":3,
         "price":30,
         "title":"item1 new name"
      },
      {
         "item_id":1,
         "quantity":16,
         "price":30,
         "title":"Test Two"
      }
   ]
}

Output with two errors about missing description fields:

[ {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/items/items"
  },
  "instance" : {
    "pointer" : "/items/0"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "missing required property(ies)",
  "required" : [ "description", "item_id", "price", "quantity", "title" ],
  "missing" : [ "description" ]
}, {
  "level" : "error",
  "schema" : {
    "loadingURI" : "#",
    "pointer" : "/properties/items/items"
  },
  "instance" : {
    "pointer" : "/items/1"
  },
  "domain" : "validation",
  "keyword" : "required",
  "message" : "missing required property(ies)",
  "required" : [ "description", "item_id", "price", "quantity", "title" ],
  "missing" : [ "description" ]
} ]

Try pasting the above into here to see the same output generated.

2 of 4
10

I realize this is an old thread, but since this question is linked from jsonschema.net, I thought it might be worth chiming in...

The problem with your original example is that you're declaring "properties" for an "array" type, rather than declaring "items" for the array, and then declaring an "object" type (with "properties") that populates the array. Here's a revised version of the original schema snippet:

"items": {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "item_id": {"type" : "number"},
            "quantity": {"type": "number"},
            "price": {"type" : "decimal"},
            "title": {"type": "string"},
            "description": {"type": "string"}
        },
        "required": ["item_id","quantity","price","title","description"],
        "additionalProperties" : false
    }
}

I would recommend against using the term "items" for the name of the array, to avoid confusion, but there's nothing stopping you from doing that...