In Swagger, how do I define a parameter as a multidimensional array? - json

In Swagger, I'm able to create a parameter that is an array of any type like this:
"MyType" : {
"description" : "my example object type",
"type" : "object",
"properties" : {
"id" : {
"description" : "identifier",
"type" : "number"
},
"data" : {
"description" : "data container",
"type" : "array",
"items" : {
"type" : "string"
}
}
}
}
Which defines an object that might look like this:
{
"id" : 1,
"data" : ["a", "b", "c"]
}
But what I need to do is define an object that might look like this:
{
"id" : 1,
"data" : [
[0, 1, 2],
["a", "b"],
[true, "foo", 99, false]
]
}
The data property needs to be a multidimensional array, and ideally it could contain any number of "rows", each with any number of columns containing any type of data in each field. I'd even settle for the schema allowing for data to just be an array of anything, but I can't figure out how to get that to work.

All you need to do is change the type of the items schema to be array. The following schema means "data" is an array, whose items are arrays. There are no constraints on the inner arrays.
"data" : {
"description" : "data container",
"type" : "array",
"items" : {
"type" : "array",
"items": {}
}
}

Related

JSON example of nullable complex type in AVRO

I created a avro schema to validate the messages that I publish on a kafka topic. These messages consist of complex types that contain other complex types. Not all fields are required, so I figured out I need to use union types in avro for that.
So basically at some point my avro schema looks like this:
"type" : "record",
"name" : "RecordA",
"fields" : [ {
"name" : "fieldX",
"type" : [ "null", {
"type" : "record",
"name" : "RecordB",
"fields" : [ {
"name" : "gemeente",
"type" : "string"
}, {
"name" : "nummer",
"type" : "string"
}, {
"name" : "postcode",
"type" : "string"
}, {
"name" : "straat",
"type" : "string"
} ]
} ]
Can someone give me an example of how a json message that adheres to this schema would look like? All the examples I found refer to simple union types that consist of primitive values.

AWS gateway - 500 error as result of misalignment between payloads (‘method executions’) and desired resource model result

We are building a final resource tree for our AWS Gateway API comprised of 4 different JSON models, each a subset of the previous one. The goal here is to bring them all into one JSON payload and execute them together successfully.
The problem is knowing what the final JSON is going to look like – we’re close, but not entirely sure. When we go and execute via the “Method Execution” we get a 500 (for failed transformation). So, again, the goal is understanding how the final JSON transformation is supposed to look like.
The first object:
"type" : "object",
"properties" : {
"identifier" : {
"type" : "string",
"description" : "unique Identifier"
},
"templateData" : {
"$ref":"~path~/models/beneObjectTemplateData"
}
}
}
The second object is a subset of “beneObjectTemplateData”:
"properties" : {
"beneData" : {
"$ref":"~path~/models/beneData"
}
},
"description" : "Beneficiary specific data"
}
The third object is a subset of “beneData”:
"type" : "object",
"properties" : {
"beneSpecific" : {
"$ref":"~path~/models/beneSpecific"
}
},
"description" : "Beneficiary data"
}
And then the last object is a subset of “beneSpecific”:
{
"type" : "object",
"properties" : {
"name" : {
"type" : "string"
},
"classification" : {
"type" : "string",
"description" : "Type of beneficiary",
"pattern" : "^(Business|Individual)$"
},
"accountNumber" : {
"type" : "string",
"description" : "Required for Wires"
},
"localAccountNumber" : {
"type" : "string",
"description" : "Required for iACH"
}
}
}
This is what we wrote out for the final transformation, but it isn’t working:
"properties": {
"identifier": "timetrail",
"templateData": {
"beneData": {
"beneSpecific": {
"name": "john",
"classification": "Individual",
"accountnumber": "3243244",
"localAccountNumber": ""
}
}
}
}
}
So, we are wondering what is going wrong here.

JSON schema connecting arrays with properties

I've been asked to create JSON Schema with filetype called "zfs" containing array property pools, where each item of such array must have properties: name (string), volumes (array of strings), sizeInGB (number from 0 to 65536), and numberOfFiles (integer from 0 to 4294967296).
Code i came up with looks like this,
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "zfs",
"type": "array",
"properties" : {
"names":{"type": "string"},
"volumes":{"type": "array"},
"properties" : {"type": "string"},
"sizeInGB":{
"type": "number",
"minimum": 0,
"maximum": 65536
},
"numberOfFiles":{
"type": "integer",
"minimum": 0,
"maximum": 4294967296
}
},
"required": [ "names", "numberOfFiles","sizeInGB","volumes"],
}
but it throws out EOF errors while validating and even though i'm aware of what that error means i just have no idea how to deal with it to make it work properly.
Maciek, there might be an issue with your schema. You define:
"type" : "array",
"properties" : {
"names" : {...},
"volumes" : {
"type" : array"
},
"properties" : { ...}
}
I understand you want an
array of objects
where each objects contains: name and an array of objects
What I would expect to be phrased in JSON Schema as:
"type" : "array",
"items" : { <============ here
"type" : "object",
"properties" : {
"names" : {...},
"volumes" : {
"type" : array"
"items" : { <============ and here
"type" : "object",
"properties" : { ... }
}
},
},
}
In JSON schema you need somehow define schema of contents of/items in an array (if you want to validate JSON array items against matching JSON schema). You can do it with "items" keyword and either using tuple syntax (if sequence of elements in array is important) or as an array of objects, where sequence is irrelevant but each object must conform to a specific schema. Yep, you can have even an array of different kinds of objects if you need to.
Please read: https://json-schema.org/understanding-json-schema/reference/array.html
and spec for https://json-schema.org/latest/json-schema-validation.html#rfc.section.6.4
Hope it helped.

JSON schema for an object whose value is an array of objects

I am writing a software that can read the JSON data from a file. The file contains "person" - an object whose value is an array of objects. I am planning to use a JSON schema validating libraries to validate the contents instead of writing the code myself. What is the correct schema that conforms to JSON Schema Draf-4 which represents the below data?
{
"person" : [
{
"name" : "aaa",
"age" : 10
},
{
"name" : "ddd",
"age" : 11
},
{
"name" : "ccc",
"age" : 12
}
]
}
The schema that wrote down is given below. I am not sure whether it is correct or is there any other form?
{
"person" : {
"type" : "object",
"properties" : {
"type" : "array",
"items" : {
"type" : "object",
"properties" : {
"name" : {"type" : "string"},
"age" : {"type" : "integer"}
}
}
}
}
}
You actually only have one line in the wrong place, but that one line breaks the whole schema. "person" is a property of the object and thus must be under the properties keyword. By putting "person" at the top, JSON Schema interprets it as a keyword instead of a property name. Since there is no person keyword, JSON Schema ignores it and everything below it. Therefore, it is the same as validating against the empty schema {} which places no restrictions on what a JSON document can contain. Any valid JSON is valid against the empty schema.
{
"type" : "object",
"properties" : {
"person" : {
"type" : "array",
"items": {
"type" : "object",
"properties" : {
"name" : {"type" : "string"}
"age" : {"type" : "integer"}
}
}
}
}
}
By the way, there are several online JSON Schema testing tools out there that can help you out when crafting your schemas. This one is my goto http://jsonschemalint.com/draft4/#
Also, here is a great JSON Schema reference that might help you out as well: https://spacetelescope.github.io/understanding-json-schema/

Different types for additionalProperties field in JSONSchema

I have to validate JSONs that look like:
{
"propertyName1" : "value",
"propertyName2" : ["value1", "value2"],
"propertyName3" : { "operator1" : "value" },
"propertyName4" : { "operator2" : ["value1", "value2"] },
...
}
So the propertyName is an arbitrary key, and operators are defined.
I think I should use a schema like:
{
"id" : "urn:my_arbitrary_json#",
"type" : "object",
"required" : false,
"additionalProperties" : {
"id" : "urn:my_arbitrary_key#",
"type" : "object",
"required" : true,
"properties" : {
"operator1" : { ... },
"operator2" : { ... }
}
}
}
However, this schema lacks definition for propertyName1 and propertyName2 cases. I would like to define an array to validate different types of additionalProperties, but this is not correct according to specification. Is there any way to validate such a JSON?
If a given piece of data can be many different shapes, then you can use oneOf or anyOf. For instance here you could have:
{
"type" : "object",
"additionalProperties" : {
"oneOf": [
{... string ...},
{... array of strings ...},
...
]
}
}
Actually, because the options here are all distinct types, you can simply have multiple entries in type instead:
{
"type" : "object",
"additionalProperties" : {
"type": ["string", "array", "object"],
"items": {"type": "string", ...}, // constraints if it's an array
"properties": {...} // properties if it's an object
}
}