Error in Parse JSON step in Microsoft Flow - json

Sorry for the long question, but I wanted to include all the JSON to get the best help.
we are getting an error parsing JSON from an HTTP Get request in our flow.
Here is the schema we are using in our Parse JSON step:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
we are only interested in the Email property that we want to use to send approvals...
here are the INPUTS results of the Parse JSON step that fails:
{
"d": {
"results": [
{
"__metadata": {
"id": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)",
"type": "SP.User"
},
"Alerts": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Alerts"
}
},
"Groups": {
"__deferred": {
"uri": "https://*********.sharepoint.com/_api/Web/GetUserById(1691)/Groups"
}
},
"Id": 1691,
"IsHiddenInUI": false,
"LoginName": "i:0#.f|membership|Surnamei#Company.org.uk",
"Title": "Firstname Surname",
"PrincipalType": 1,
"Email": "Surnamef#Company.org.uk",
"Expiration": "",
"IsEmailAuthenticationGuestUser": false,
"IsShareByEmailGuestUser": false,
"IsSiteAdmin": false,
"UserId": {
"__metadata": {
"type": "SP.UserIdInfo"
},
"NameId": "10033fff9fe67a30",
"NameIdIssuer": "urn:federation:microsoftonline"
},
"UserPrincipalName": "Surnamef#Company.org.uk"
},
}
]
}
here is the schema from the run results of the Parse JSON that fails:
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Alerts": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Groups": {
"type": "object",
"properties": {
"__deferred": {
"type": "object",
"properties": {
"uri": {
"type": "string"
}
}
}
}
},
"Id": {
"type": "integer"
},
"IsHiddenInUI": {
"type": "boolean"
},
"LoginName": {
"type": "string"
},
"Title": {
"type": "string"
},
"PrincipalType": {
"type": "integer"
},
"Email": {
"type": "string"
},
"IsEmailAuthenticationGuestUser": {
"type": "boolean"
},
"IsShareByEmailGuestUser": {
"type": "boolean"
},
"IsSiteAdmin": {
"type": "boolean"
},
"UserId": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"type": {
"type": "string"
}
}
},
"NameId": {
"type": "string"
},
"NameIdIssuer": {
"type": "string"
}
}
}
},
"required": [
"__metadata",
"Alerts",
"Groups",
"Id",
"IsHiddenInUI",
"LoginName",
"Title",
"PrincipalType",
"Email",
"IsEmailAuthenticationGuestUser",
"IsShareByEmailGuestUser",
"IsSiteAdmin",
"UserId"
]
}
}
}
}
}
}
and here is the error in the step from the run history:
[
{
"message": "Invalid type. Expected Object but got Null.",
"lineNumber": 0,
"linePosition": 0,
"path": "d.results[3].UserId",
"schemaId": "#/properties/d/properties/results/items/properties/UserId",
"errorType": "type",
"childErrors": []
}
]
so how can I change the schema to successfully retrieve the email

The problem was caused by UserId being null in several elements of the data received from the HTTP Get, while that is set as required in the JSON Schema...
initially we solved it by using the below in the HTTP Get:
_api/web/sitegroups/getbyname('Legal Owners')/users?$filter=UserId+ne+null
Then we were given a better and more elegant solution on the Power Platform Community:
in the Apply to each step use:
body('Send_an_HTTP_request_to_SharePoint')?['d']?['results']
and within the loop use:
item()?['Email']

Related

Error while creating models for request validation in aws api gateway

I am trying to build model for the request validation in the aws api gateway that i created.
In the model schema, when i use simple json schema, it works. But when i use my schema as below.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"uuid": {
"type": "string",
"pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
"minLength": 36,
"maxLength": 36
},
"macAddress": {
"type": "string",
"pattern": "([0-9a-fA-F]{2}[:]){5}([0-9a-fA-F]{2})"
},
"locale": {
"type": "string",
"pattern": "^[a-z]{2}(-[A-Z]{2})?$",
"minLength": 2
},
"status": {
"type": "string",
"enum": ["good", "bad", "canceled"]
},
"timezone": {
"type": "string",
"pattern": "^[A-Z][a-z]+/[A-Z][a-z]+$"
},
"device": {
"type": "object",
"additionalProperties": false,
"properties": {
"mac_address": {
"type": "string",
"$ref": "#/definitions/macAddress"
},
"hardware": {
"type": "string"
}
},
"required": ["mac_address"]
},
"started_at": {
"type": "number",
"minimum": 0,
"maximum": 2147483647999
},
"finished_at": {
"type": "number",
"minimum": 0,
"maximum": 2147483647999
},
"questionType": {
"enum": [
"barcode",
"name",
"text"
]
},
"question": {
"type": "object",
"additionalProperties": false,
"properties": {
"key": {
"string"
},
"type": {
"$ref": "#/definitions/questionType"
}
},
"required": ["key", "type"]
},
"answers": {
"type": "array",
"items": {
"type": "object",
"properties": {
"question": {
"$ref": "#/definitions/question"
},
"status": {
"type": "string",
"$ref": "#/definitions/status"
},
"started_at": {
"type": "number",
"$ref": "#/definitions/started_at"
},
"finished_at": {
"type": "number",
"$ref": "#/definitions/finished_at"
}
},
"allOf": [
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "text" }
} } } },
"then": {
"properties": { "value": {"type": "string"} } }
},
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "name" }
} } } },
"then": {
"properties": { "value": {"type": "string" } } }
},
{"if": {
"properties": { "question": { "properties": {
"type": { "const": "barcode" }
}} } },
"then": {
"properties": { "value": {"type": "string" } } }
}
]
}
}
},
"properties": {
"locale": {
"$ref": "#/definitions/locale"
},
"timezone": {
"$ref": "#/definitions/timezone"
},
"device": {
"$ref": "#/definitions/device"
},
"answers": {
"$ref": "#/definitions/answers"
}
},
"required": ["locale"]
}
I get the below error.
validation error detected: Value 'Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified. Unsupported keyword(s): ["if","then"], Invalid model schema specified.
When i read the aws documentation, i see that the json schema draft 4 is supported and i have also used the draft 4 rules to build my schema.
Is there a hack to use the if/then in aws model schema or how can i build the schema without if then?

How to prevent additions properties when using oneOf in JSON Schema

I have a JSON configuration with an array of "specs", with many different kinds of "path" properties. The "kind" property is the selector of the variant.
I want to restrict the validation to not allow addition properties. In the JSON example, the properties "noAllowedInt" and "notAllowedObject" must report a validation error.
How can I add this rule to the JSON schema?
{
"specs": [
{
"id": 12,
"label": "Serial",
"path": {
"kind": "serial",
"speed": 9600,
"parity": "even"
}
},
{
"id": 13,
"label": "Memory",
"path": {
"kind": "memory",
"storage": "permanent",
"location": "external"
},
"noAllowedInt": 42,
"notAllowedObject": {
"value": 3.1415
}
}
]
}
There are two Json Schema to validate this, a simple one and one with "definitions"
Simple JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
{
"type": "object",
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
]
}
}
},
"required": [
"specs"
]
}
JSON Schema with "definitions"
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": true,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
}
},
"required": [
"id",
"label"
],
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
}
},
"required": [
"specs"
],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"serial"
]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
},
"PathKindMemory": {
"type": "object",
"additionalProperties": true,
"properties": {
"path": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [
"memory"
]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [
"kind"
]
}
}
}
}
}
I guess you need to keep the path definition as a property at the specs array level. This would be my proposal:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"type": "object",
"additionalProperties": false,
"properties": {
"specs": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"id": {
"type": "integer"
},
"label": {
"type": "string"
},
"path": {
"oneOf": [
{
"$ref": "#/definitions/PathKindSerialType"
},
{
"$ref": "#/definitions/PathKindMemory"
}
]
}
},
"required": [ "id", "label" ]
}
}
},
"required": [ "specs" ],
"definitions": {
"PathKindSerialType": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "serial" ]
},
"speed": {
"type": "integer"
},
"parity": {
"type": "string"
}
},
"required": [ "kind" ]
},
"PathKindMemory": {
"type": "object",
"additionalProperties": false,
"properties": {
"kind": {
"type": "string",
"enum": [ "memory" ]
},
"storage": {
"type": "string"
},
"location": {
"type": "string"
}
},
"required": [ "kind" ]
}
}
}

Assistance needed to create JSON schema

As I am new to JSON schema creation, I have learnt the basics of JSON schema and now I am trying to create JSON schema for the below mentioned set of data,
{
"Result": [
{
"ResourceName": "Appointment",
"Sequence": "1",
"Data": {
"AppointmentID": "A1234",
"PatientName": "Test Patient",
"ClinicName": "Test Clinic"
}
},
{
"ResourceName": "EpisodeofCare",
"Sequence": "2",
"Data": {
"EpisodeID": "EP1234",
"LocationId": "L1234",
"AppointmentId": "A1234",
"TransactionStatus": "2",
"OPNumber": "OP523367"
}
},
{
"ResourceName": "Encounter",
"Sequence": "3",
"Data": {
"EncounterID": "E1234",
"PatientID": "P1234"
}
}
]
}
Can anybody please help me to create JSON schema for this kind of data set.
Thanks in advance for helping me out on this.
Below is the JSON schema i have drafted
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ResultType": {
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
}
},
"Appointment": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "boolean"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
},
"EpisodeofCare": {
"type": "object",
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
}
},
"Encounter": {
"type": "object",
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
}
}
},
"type": "object",
"properties": {
"Result": {
"type": "array",
"$ref": "#/definitions/ResultType"
}
}
}
Throw it in a schema validator like this one.
You will see errors in your schemas definition and in validation.
First of all check that your schema is valid json (the validator might help):
[
{
{"$ref": "#/definitions/Appointment"} <- unnecessary and invalid braces
},
{
{"$ref": "#/definitions/EpisodeofCare"} <- unnecessary and invalid braces
},
{
{"$ref": "#/definitions/Encounter"} <- unnecessary and invalid braces
}
]
Within the Result property you want each item of the array. You should use the item property instead of ref. As taken from the specification:
The value of "items" MUST be either a valid JSON Schema or an array of valid JSON Schemas.
[...]
If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.
Thus you should use
{
"type": "object",
"properties": {
"Result": {
"type": "array",
"items": {
"$ref": "#/definitions/ResultType"
}
}
}
}
Since the schema should be as specific as possible you should constraint the content of the ResourceName properties to the actual data layout.
Use the const property to define a constant value for the ResourceName for each type of Resource. Take for example the Appointment type, which would change to:
"Appointment": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Appointment"
},
"Data": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
}
}
I also changed the type of AppointmentID to string.
Then your ResultType needs to be adjusted. Before you used anyOf to validate the data, now you need it to validate the complete object while still maintaining the overall structure:
"ResultType": {
"allOf": [
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": true
}
},
{
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
]
}
The allOf propertiy implies a and condition. The overall structure (the object) does not care what value ResourceName has or how Data is structured. The specific implementations (the anyOf part) take care of that.
Some final improvements. Use the required property to define the names of properties that must exist in your JSON file. Also add "additionalProperties": false to ensure that only the specified properties are used. And finally constraint your sequence values to be string representations of positive intergers by adding "pattern": "[1-9]\\d*".
Final result:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"ResultType": {
"allOf": [
{
"type": "object",
"required": [
"ResourceName",
"Sequence",
"Data"
],
"additionalProperties": false,
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string",
"pattern": "[1-9]\\d*"
},
"Data": true
}
},
{
"anyOf": [
{
"$ref": "#/definitions/Appointment"
},
{
"$ref": "#/definitions/EpisodeofCare"
},
{
"$ref": "#/definitions/Encounter"
}
]
}
]
},
"Appointment": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Appointment"
},
"Data": {
"type": "object",
"required": [
"AppointmentID",
"PatientName",
"ClinicName"
],
"additionalProperties": false,
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
}
}
}
},
"EpisodeofCare": {
"type": "object",
"properties": {
"ResourceName": {
"const": "EpisodeofCare"
},
"Data": {
"type": "object",
"required": [
"EpisodeID",
"LocationId",
"AppointmentId",
"TransactionStatus",
"OPNumber"
],
"additionalProperties": false,
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
}
}
}
},
"Encounter": {
"type": "object",
"properties": {
"ResourceName": {
"const": "Encounter"
},
"Data": {
"type": "object",
"required": [
"EncounterID",
"PatientID"
],
"additionalProperties": false,
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
}
}
}
}
},
"type": "object",
"required": [
"Result"
],
"additionalProperties": false,
"properties": {
"Result": {
"type": "array",
"items": {
"$ref": "#/definitions/ResultType"
}
}
}
}
you can create json schema by online tools which generates json schema from the json
like: https://www.liquid-technologies.com/online-json-to-schema-converter , it also checks if json is valid or not.
Json Schema that you need:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Result": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"AppointmentID": {
"type": "string"
},
"PatientName": {
"type": "string"
},
"ClinicName": {
"type": "string"
}
},
"required": [
"AppointmentID",
"PatientName",
"ClinicName"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
},
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"EpisodeID": {
"type": "string"
},
"LocationId": {
"type": "string"
},
"AppointmentId": {
"type": "string"
},
"TransactionStatus": {
"type": "string"
},
"OPNumber": {
"type": "string"
}
},
"required": [
"EpisodeID",
"LocationId",
"AppointmentId",
"TransactionStatus",
"OPNumber"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
},
{
"type": "object",
"properties": {
"ResourceName": {
"type": "string"
},
"Sequence": {
"type": "string"
},
"Data": {
"type": "object",
"properties": {
"EncounterID": {
"type": "string"
},
"PatientID": {
"type": "string"
}
},
"required": [
"EncounterID",
"PatientID"
]
}
},
"required": [
"ResourceName",
"Sequence",
"Data"
]
}
]
}
},
"required": [
"Result"
]
}

Create a recursive json schema which is generic

I am trying to create a json schema for a json file. The structure of the json is generic and I am trying to create a generic schema for json files which contain a similar structure to the one shown below.
**Json file:**
{
"Soccer League": {
"lvl": "Championships",
"state": "1",
"Clubs": {
"Rosely": {
"Boys": {"id": "A1", "state": "Ready"},
"Girls": {
"id": "A2",
"state": "Ready",
"Substitutes": {
"id": "A3",
"state": "Sitting",
"Goalkeepers": {
"Players": {"id": "A4", "state": "Idle"},
"id": "A5",
"state": "Idle",
},
},
},
},
"Division League": {
"TeamA": {
"PlayersA": {
"id": "A6",
"status": "Ready",
"CoachA": {"id": "A7", "state": "Ready"},
},
"PlayersB": {
"id": "A8",
"state": "Playing",
"CoachB": {"id": "A9", "state": "Idle"},
},
}
},
},
}
}
**Json-schema:**
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Soccer League": {
"type": "object",
"properties": {
"lvl": {
"type": "string"
},
"state": {
"type": "string"
},
"Clubs": {
"type": "object",
"properties": {
"Rosely": {
"type": "object",
"properties": {
"Boys": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"id",
"state"
]
},
"Girls": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
},
"Substitutes": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
},
"Goalkeepers": {
"type": "object",
"properties": {
"Players": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"id",
"state"
]
},
"id": {
"type": "string"
},
"state": {
"type": "string"
}
},
"required": [
"Players",
"id",
"state"
]
}
},
"required": [
"id",
"state",
"Goalkeepers"
]
}
},
"required": [
"id",
"state",
"Substitutes"
]
}
},
"required": [
"Boys",
"Girls"
]
},
"Division League": {
"type": "object",
"properties": {
"TeamA": {
"type": "object",
"properties": {
"PlayersA": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
},
"CoachA": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"id",
"status"
]
}
},
"required": [
"id",
"status",
"CoachA"
]
},
"PlayersB": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
},
"CoachB": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"id",
"status"
]
}
},
"required": [
"id",
"status",
"CoachB"
]
}
},
"required": [
"PlayersA",
"PlayersB"
]
}
},
"required": [
"TeamA"
]
}
},
"required": [
"Rosely",
"Division League"
]
}
},
"required": [
"lvl",
"state",
"Clubs"
]
}
},
"required": [
"Soccer League"
]
}
As we can see from above, we have many same layers but just with different names. I am unsure on how to create a recursive json schema for json files which will follow a similar format. This is sort of hard-coded, but how to make it generic.
I am new to json schema and would appreciate if someone could help me infer a schema from the json above. Any help would be greatly appreciated.

How to use JSON schema validation against JSON document having arrays?

I have json document which contains three main elements along with sub elements. These elements also have arrays. I validated this document with json schema but I am not sure if there is any simple way to get rid of repeating schema for arrays as the current schema is too long.
JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "/",
"type": "object",
"properties": {
"book": {
"id": "book",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
},
"required": [
"isbn",
"title",
"price",
"categories",
"category",
"warehouse"
]
},
{
"id": "1",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"isbn": {
"id": "isbn",
"type": "string"
},
"title": {
"id": "title",
"type": "string"
},
"price": {
"id": "price",
"type": "string"
},
"categories": {
"id": "categories",
"type": "array",
"items": [
{
"id": "0",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "1",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
},
{
"id": "2",
"type": "object",
"properties": {
"category": {
"id": "category",
"type": "string"
}
}
}
]
},
"warehouse": {
"id": "warehouse",
"type": "object",
"properties": {
"location": {
"id": "location",
"type": "string"
},
"aisle": {
"id": "aisle",
"type": "string"
},
"shelf": {
"id": "shelf",
"type": "string"
}
}
}
}
}
],
"required": [
"0",
"1",
"2"
]
}
},
"required": [
"book"
]
}
And JSON document:
{
"book":[
{
"isbn":"0-672-33751-7",
"title":"Unity Game Development",
"price":"$55.99",
"categories":[
{
"category":"Game Development"
},
{
"category":"Unit Game Engine"
},
{
"category":"Beginner to Intermediate"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"A16",
"shelf":"3"
}
},
{
"isbn":"978-0-9871530-7-4",
"title":"Jquery: Novice to Ninja",
"price":"$39.95",
"categories":[
{
"category":"JavaScript"
},
{
"category":"jQuery"
},
{
"category":"Web Development"
}
],
"warehouse":{
"location":"North Warehouse",
"aisle":"W03",
"shelf":"6"
}
},
{
"isbn":"0-538-74477-4",
"title":"Programming Logic and Design",
"price":"$89.99",
"categories":[
{
"category":"Programming"
},
{
"category":"JavaScript"
},
{
"category":"Computer Logic"
}
],
"warehouse":{
"location":"South Warehouse",
"aisle":"B44",
"shelf":"1"
}
}
]
}
You've got a lot of issues with this schema. I suggest you read http://spacetelescope.github.io/understanding-json-schema/ to get a better understanding of JSON Schema. Below is the corrected schema. I'll give some brief advice, but not a lot of explanation. Go read that link for more info.
id is required to be a absolute URL. "/" is not valid
Don't use id anywhere other than the root of the document. It has some unexpected properties. It's best to just avoid it.
The items keyword should be a schema not an array of schemas. The form you are using is not intended for arrays with a single type of item.
The required keyword applies only to properties in the schema it appears in. Sub-schemas need to define their own required properties.
The required keyword does not work on arrays. Use minItems and maxItems instead.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"book": {
"type": "array",
"items": {
"type": "object",
"properties": {
"isbn": { "type": "string" },
"title": { "type": "string" },
"price": { "type": "string" },
"categories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"category": { "type": "string" }
},
"required": ["category"]
}
},
"warehouse": {
"type": "object",
"properties": {
"location": { "type": "string" },
"aisle": { "type": "string" },
"shelf": { "type": "string" }
}
}
},
"required": [
"isbn", "title", "price", "categories", "warehouse"
]
}
}
},
"required": ["book"]
}