I was given a JSON schema which uses "oneOf" like below, is there a way to define XSD to generate the JAXB objects, which can support this "oneOf" definition of JSON data marshalling and demarshalling? I am using Jackson.
"Source": {
"description": "Indicates the source.",
"type": "object",
"oneOf": [
{
"properties": {
"Source1": {
"description": "Indicates the source 1.",
"type": "string"
}
},
"additionalProperties": false
},
{
"properties": {
"Source2": {
"description": "Indicates the source 2",
"type": "object",
"properties": {
"field1": {
"description": "filed 1 for source 1",
"type": "string"
},
"field2": {
"description": "field 2 for source 2",
"type": "string"
}
}
}
},
"additionalProperties": false
}
]
}
Related
I have JSON data of which is an array of data like
[
{
"type": "background_color",
"data": {
"backgroundColor": "F9192D"
}
},
{
"type": "banner_images",
"data": {
"images": [
{
"url": "https://example.com/abc.jpg",
"id": 3085
},
{
"url": "https://example.com/zyx.jpg",
"id": 3086
}
]
}
},
{
"type": "description_box",
"data": {
"text": "Hello 56787"
}
}
]
The data is an array of object which has two keys type and data. The type and keys of the data will be defined by the type of data it has.
Like for background_color type, the data should have backgroundColor property, while for banner_images, data should have images which is an array of other properties.
Till now, What I have done is
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"title": "category schema",
"description": "Used to validate data of category",
"examples": [],
"required": [],
"items": {
"type": "object",
"required": [
"type",
"data"
],
"properties": {
"type": {
"type": "string",
"enum": ["background_color", "banner_images", "description_box"]
},
"data": {
"type": "object" // How to define data property here for each use case
}
}
}
}
I'm not getting how to define the data property for each use case?
You can use if/then/else blocks to define conditional constraints.
The values of if and then are schemas. If the if schema is valid, then the then schema is applied, otherwise, the allOf subschema (allOf[0] in this example) would pass validation.
There are a few different ways to do this, but this is clean when you don't have any additional or special requirements. Please come back if you do =]
In this example, I've added banner_images...
You can test it working here.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"title": "category schema",
"description": "Used to validate data of category",
"items": {
"type": "object",
"required": [
"type",
"data"
],
"properties": {
"type": {
"type": "string",
"enum": [
"background_color",
"banner_images",
"description_box"
]
},
"data": {
"type": "object"
}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "banner_images"
}
}
},
"then": {
"properties": {
"data": {
"required": [
"images"
],
"properties": {
"images": {
"type": "array"
}
}
}
}
}
}
]
}
}
For reference, here's the part of the JSON Schema draft-7 spec document that details the behaviour: https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.6
I'm creating some models in AWS API Gateway. I'm having problems with one that I wish it receives 2 input formats: one of the formats is just a dictionary the other is an array of dictionaries:
{
"id":"",
"name":""
}
and
[
{
"id":"",
"Family":""
},
{
"id":"",
"Family":""
},
...
{
"id":"",
"Family":""
}
]
Until now I've created the model to accept only the dictionary way:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
Can you give me some tips to create the array of dictionaries, please. I've done some research and I found nothing but I'm following the way of the keywords oneOf and anyOf but I'm not sure.
You're on the right track with anyOf. What you should do depends on the similarity between the object (dictionary) that's by itself and the object that's in the array. They look different in your example, so I'll answer in kind, then show how to simplify things if they are in fact the same.
To use anyOf, you want to capture the keywords that define your dictionary
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
and wrap that inside an anyOf right at the root level of the schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"anyOf": [
{
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
]
}
To write a schema for an array of the same kind object, you need the items keyword.
{
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "string"},
"Family": { "type": "string"}
},
"required": ["id"]
}
}
Add this in as a second element in the anyOf array, and you're golden.
If your lone object can have the same schema as your array-element object, then you can write that schema once as a definition and reference it in both places.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update",
"definitions": {
"myObject": {
"type": "object",
"properties": {
"id": { "type": "string"},
"name": { "type": "string"}
},
"required": ["id"]
}
},
"anyOf": [
{ "$ref": "#/definitions/myObject" },
{
"type": "array",
"items": { "$ref": "#/definitions/myObject" }
}
]
}
Background
I have a custom connector which returns a JSON response .I am trying to parse the response to JSON since i want to use the response later in other flows. So that I am using Parse JSON Action from Data operations connector. Following is the JSON response and the JSON schema i provided to Parse JSON.
Response
[
[
{
"key":"Customer_Key",
"value":{
"id":"abfa48ad-392d-e511-80d3-005056b34214",
"name":"90033"
}
},
{
"key":"Status",
"value":"Done"
}
]
]
Schema
{
"type": "array",
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
},
"required": [
"key",
"value"
]
}
}
}
Exception
{
"message": "Invalid type. Expected Object but got String.",
"lineNumber": 0,
"linePosition": 0,
"path": "[0][2].value",
"value": "90033",
"schemaId": "#/items/items/properties/value",
"errorType": "type",
"childErrors": []
},
Any one knows what is the issue on this ?How we can I convert above json response
Looks like the Use sample payload to generate schema couldn't generate right schema.
So you could go to this liquid studio site and paste the JSON payload then click the Generate Schema button, then you will get the Json Schema.
And I test the Schema, it worked perfectly.
Hope this could help you, if you still have other questions,please let me know.
Schema looks incorrect. try with the below schema:
{
"type": "array",
"items": [
{
"type": "array",
"items": [
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"id",
"name"
]
}
},
"required": [
"key",
"value"
]
},
{
"type": "object",
"properties": {
"key": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": [
"key",
"value"
]
}
]
}
]
}
I have a JSON file and i want to validate the file using JSON Schema on MongoDB.
How do i import the JSON Schema to MongoDB and then validate the JSON File.
JSON file is already in one collection so i want to validate without importing a new JSON file.
JSON:
{
"Book": {
"Year:2016-2017": {
"Crs": [{
"Cr": {
"_id": {
"$oid": "5a439ff4fc0900f06fb470a4"
},
"Number": 35,
"Pag": 8,
"Desc": "Embl",
"Ad": "S",
"Type": "Embl"
}
}]
}
}
}
JSON schema:
{
"type": "object",
"$schema": "http://json-schema.org/draft-03/schema",
"id": "http://jsonschema.net",
"required": false,
"properties": {"Book": {
"type": "object",
"id": "http://jsonschema.net/Book",
"required": false,
"properties": {"Year:2016-2017": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017",
"required": false,
"properties": {"Crs": {
"type": "array",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs",
"required": false,
"items": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0",
"required": false,
"properties": {"Cr": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr",
"required": false,
"properties": {
"Ad": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Ad",
"required": false
},
"Desc": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Desc",
"required": false
},
"Number": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Number",
"required": false
},
"Pag": {
"type": "number",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Pag",
"required": false
},
"Type": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/Type",
"required": false
},
"_id": {
"type": "object",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id",
"required": false,
"properties": {"$oid": {
"type": "string",
"id": "http://jsonschema.net/Book/Year:2016-2017/Crs/0/Cr/_id/$oid",
"required": false
}}
}
}
}}
}
}}
}}
}}}
I want to validate the JSON using the schema with mongodb.
JSON file is already in one collection so i want to validate without importing a new JSON file.
As of MongoDB v3.6.x, document schema validation only occurs during updates and inserts, existing documents do not undergo validation checks until modification. This means that your existing JSON document in a MongoDB collection will not be validated by the validation rule(s) that you just applied.
Note that $jsonSchema operator and JSON-SCHEMA support is new in MongoDB version 3.6.
i want to add a new "Cr" but it needs to respect the JSON Schema.(Insertion/Update)
Once you specified a document schema validation on a collection, any update operations will trigger validation checks on the affected documents.
For example, if you have an existing document in a collection as below:
{
"_id": 1,
"a": {
"b": {
"crs": [
{
"cr": {
"d": 2,
"e": "two"
}
}
]
}
}
}
If you then apply a validator as below:
// Validator for nested array of objects.
var schema = {
"type": "object",
"properties": {
"a": {
"type": "object",
"properties": {
"b": {
"type": "object",
"properties": {
"crs": {
"type": "array",
"items": {
"type": "object",
"properties": {
"cr": {
"type": "object",
"properties": {
"d": {
"bsonType": "int",
},
"e": {
"type": "string",
}
}
}}
}
}}
}}
}}}
db.runCommand({"collMod": "collectionName",
"validator": { "$jsonSchema": schema}}
);
When you update the existing document to add a new cr, the whole document will be validated.
db.collectionName.update({_id: 1}, {"$push":{
"a.b.crs":{
"cr":{
"d": NumberInt(20),
"e": "new element"}}}
});
Any pre-existing documents that are not valid according to your document schema validation rule(s), you will have to correct the documents first before updating them.
I need to create a JSON schema for object that will include java Properties object as one of its properties.
The nested Properties object will be simply list of key=value. Both key and value are of type string.
I failed to find any docs that describe how to define the schema that includes 2 new types.
shall it be something like:
{
"type": "object",
"name": "MyObj",
"properties": {
"prop1": {
"type": "string",
"description": "prop1",
"required": true
},
"props": {
"type": "array",
"items": {
"type": "object"
"properties": {
"key": {
"type": "string",
"description": "key",
"required": true
},
"value": {
"type": "string",
"description": "the value",
"required": true
}
}
"description": "the value",
"required": true
}
}
}
}
The schema you have written (assuming the commas are fixed) describes data of the form:
{
"prop1": "Some string property goes here",
"props": [
{"key": "foo", "value": "bar"},
{"key": "foo2", "value": "bar2"},
...
]
}
If this is what you wanted, then you are already finished.
However, I do wonder why you are using key/value pairs in an array, when you could use a JSON object with string keys instead. Using the additionalProperties keyword, you could have a schema:
{
"type": "object",
"name": "MyObj",
"properties": {
"prop1": {
"type": "string",
"description": "prop1"
},
"props": {
"type": "object",
"additionalProperties": {
"type": "string",
"description": "string values"
}
}
}
}
This describes a data format like:
{
"prop1": "Some string property goes here",
"props": {
"foo": "bar",
"foo2": "bar2"
}
}
At W3 schools (JSON Syntax) you can read how the array should be defined.
There is no schema like the xsd for xml, however i've found an approach on json-schema.org. If you are able to, i'll advice to youse google-GSON library for JSON. You could Store key Value as "id" : "value" and build only one object, containing all requieed pairs:
{ "lang" : "EN" , "color" : "red" }
Your posted model is incorect, you can check it on jsonlint.com
Here is a working version, i'm not sure if the modell is as expected.
{
"type": "object",
"name": "MyObj",
"properties": [
{
"prop1": {
"type": "string",
"description": "prop1",
"required": true
},
"props": {
"type": "array",
"items": {
"type": "object",
"properties": {
"key": {
"type": "string",
"description": "key",
"required": true
},
"value": {
"type": "string",
"description": "the value",
"required": true
}
},
"description": "the value",
"required": true
}
}
}
]
}