Saturday, November 16, 2024
Google search engine

JSON Schema

JSON Schema is a content specification language used for validating the structure of a JSON data.It helps you specify the objects and what values are valid inside the object’s properties. JSON schema is useful in offering clear, human-readable, and machine-readable documentation.

Structure of a JSON Schema: Since JSON format contains an object, array, and name-value pair elements. Name-value pairs are used for providing schema processing elements as well as validating the JSON content. Schema processing elements include(not limited to).

  • $schema” To specify the version of JSON schema.
  • title and description: To provide information about the schema.
  • required: It’s an array of elements that indicates which elements should be present.
  • additionalProperties: To indicate whether existence of specified elements are allowed or not.

JSON content definition:

  • When a JSON object is defined, JSON schema uses name-value pair “type”:”object”
  • When arrays are defined, JSON schema uses the name-value pair “type”:”array”

Example:

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Voters information",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name."
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name."
    },
    "age": {
      "description": "Age in years which must be equal to or 
                      greater than eighteen in order to vote.",
      "type": "integer",
      "minimum": 18
    }
  }
}

Output:

{
  "firstName": "Indra",
  "lastName": "Sen",
  "age": 20
}

The above JSON schema contains the following:

  • $id keyword
  • $schema keyword
  • title annotation keyword
  • properties validation keyword
  • Three keys: firstName, lastName and age each with their own description keyword.
  • type instance data model (see above)
  • minimum validation keyword on the age key.

RELATED ARTICLES

Most Popular

Recent Comments