I'm familiar with json schemas for arrays of a single object type. E.g. for this json example:
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 1,
"Steps": {
"steps": 4000
}
}
]
}
...the associated schema format (that I've been introduced to) might be:
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
}
}
}
}
}
}
But how can I adapt this schema to allow for objects of different types, e.g.
{
"arr": [
{
"type": 1,
"Steps": {
"steps": 3500
}
},
{
"type": 2,
"HeartRate": {
"heartrates": 4000
}
}
]
}
?
Example adapted from Parse json array with different objects to their classes
I'd like the schema validation to pass at https://www.jsonschemavalidator.net/
I don't understand why, but the second example above passes at the noted online json schema validator using the above schema. Regardless of why it passes, I don't think the schema is correct, because it doesn't represent that there can be "Heartrate" objects.
I did read JSON Schema - array of different objects, but I believe this is not a dup, because that Q&A is about "Swagger 2.0" documents, which I'm not working with.
Update:
Would this be a reasonable/correct schema?
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"type": "object",
"properties": {
"arr": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "integer"
},
"Steps": {
"type": "object",
"properties": {
"steps": {
"type": "integer"
}
}
},
"HeartRate": {
"type": "object",
"properties": {
"heartrates": {
"type": "integer"
}
}
}
}
}
}
}
}
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
"?