JSON Schema

JSON Schema is used to format or structure the JSON data and it progresses the validation constraints of JSON documents. JSON Schema is a provision term for JSON-based format to explain the structure of JSON data or documents. This JSON Schema is written with the draft V4 Specification; it is also shown with the $schema keyword. JSON Schema documents that describe other JSON documents and it is as simple as that, so if we have to define or describe JSON Schema.

Core stuff of JSON Schema is Schema Validation and Hyper Schema. Here Core usually defines the terminology of JSON and it describes the normal mechanisms. Both JSON cores use the terminology as defined in the core.

json schema

Validation progress with JSON data:-

  • JSON Schema format that can validate and structure the JSON document or the JSON message in simple words or string type.
  • We can use JSON Schema for validating the formatting of a JSON message, the syntax of the data types, and to validate the entire structure and content of a JSON message (Note:-JSON schema is written in JSON itself.)
  • It can be used to verify a JSON message for further process of structuring Schema.
  • APIs that have a JSON request and response.
  • JSON Schema can be used to validate the API request and response.
  • when we run our API requests or do any process or action on the request we get the output and finally get the API response.
  • If we do any processing or any action on the API request we can validate the format so that we can save a lot of time.
  • If we face any issue with the format syntax or data types of the API’s request. We can catch it right at the stage of validating the format.
  • Before we send out the response we can again validate the format of the response and this validation of the format is done by JSON Schema.

JSON Schema Format Validation Process:-

  • API request
  • Validating format with JSON Schema
  • Processing
  • Output
  • Validate format with JSON Schema

JSON Schema Example

Here we see basic json schema examples

{
  "$id": "https://google.com/schema.json",
  "$schema": "https://jsontostring.com/schema",
  "title": "Book",
  "type": "object",
  "properties": {
    "bookName": {
      "type": "string",
      "description": "The Book Name"
    },
    "authorName": {
      "type": "string",
      "description": "The Author Name"
    },
    "order": {
      "description": "order in numbers which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0
    }
  }
}

Data

{
  "bookName": "In Search of Lost Time",
  "authorName": "Marcel Proust",
  "order": 1
}

Array JSON Schema Example

{
  "$id": "https://google.com/schema.json",
  "$schema": "https://jsontostring.com/schema",
  "description": "A representation of a object",
  "type": "object",
  "properties": {
    "books": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "bookName": {
      "type": "array",
      "items": { "$ref": "#/$defs/book" }
    }
  },
  "$defs": {
    "book": {
      "type": "object",
      "required": [ "bookName", "bookLike" ],
      "properties": {
        "bookName": {
          "type": "string",
          "description": "The name of the books."
        },
        "bookLike": {
          "type": "boolean",
          "description": "Do I like this books?"
        }
      }
    }
  }
}

Data


{
  "books": [ "In Search of Lost Time", "Ulysses", "Don Quixote" ],
  "book": [
    {
      "bookName": "In Search of Lost Time",
      "bookLike": true
    },
    {
      "bookName": "Ulysses",
      "bookLike": false
    }
  ]
}

Hyper-Schema of JSON Schema:-

  1. Hyper-Shema reports the hyperlink and hypermedia relation keywords.
  2. The Keywords used with JSON Schema are links, ref, and href keywords.
  3. By these keywords, we are defining the hyper-text and hyper-media references.
  4. These all hyper-text and hyper-media references core-up by using encoded strings.
  5. Hyper Schema's main aim is to play a central role in the JSONForms.

Related Articles: