Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema definition for array of objects

I've seen this other question but it's not quite the same, and I feel like my issue is simpler, but just isn't working.

My data would look like this:

[     { "loc": "a value 1", "toll" : null, "message" : "message is sometimes null"},     { "loc": "a value 2", "toll" : "toll is sometimes null", "message" : null} ] 

I'm wanting to use AJV for JSON validation in a Node.js project and I've tried several schemas to try to describe my data, but I always get this as the error:

[ { keyword: 'type',     dataPath: '',     schemaPath: '#/type',     params: { type: 'array' },     message: 'should be array' } ] 

The schema I've tried looks like this:

{   "type": "array",   "items": {     "type": "object",     "properties": {       "loc": {         "type": "string"       },       "toll": {         "type": "string"       },       "message": {         "type": "string"       }     },     "required": [       "loc"     ]   } } 

I've also tried to generate the schema using this online tool but that also doesn't work, and to verify that that should output the correct result, I've tried validating that output against jsonschemavalidator.net, but that also gives me a similar error:

Found 1 error(s)  Message:  Invalid type. Expected Array but got Object.  Schema path:  #/type 
like image 807
Kyle Falconer Avatar asked Apr 21 '16 01:04

Kyle Falconer


People also ask

How do you define an array of objects in JSON?

Array Datatype in JSON Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma. The array index begins with 0. The square brackets [...] are used to declare JSON array.

What is JSON object schema?

JSON Schema is a grammar language for defining the structure, content, and (to some extent) semantics of JSON objects. It lets you specify metadata (data about data) about what an object's properties mean and what values are valid for those properties.

What is a schema array?

Array Schemas Arrays are used to represent ordered sets of values, such as the following sequence of strings: ["Chilean", "Argentinean", "Peruvian", "Colombian"] In this section we specify array's main charasteristics and restrictions that may apply to them using a single JSON Schema document.

Can array elements be objects in JSON?

In JSON, array values must be of type string, number, object, array, boolean or null.


1 Answers

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?

like image 199
Jason Desrosiers Avatar answered Sep 21 '22 17:09

Jason Desrosiers