Skip to content

What JSON Schema and how make it

What JSON Schema

JSON Schema is a vocabulary that allows you to describe and validate the structure, content, and format of JSON documents. It provides a way to define the rules and constraints that JSON data must adhere to, enabling automated validation, documentation generation, and interoperability between systems.

At its core, a JSON Schema is itself a JSON document that describes the expected shape and properties of other JSON documents. It defines the following aspects of a JSON document:

  • Data Types: JSON Schema allows you to specify the data types of properties in a JSON document, such as strings, numbers, booleans, arrays, and objects.

  • Constraints and Validation Rules: JSON Schema enables you to define constraints and validation rules for each property, such as minimum and maximum values, string length limits, allowed values, regular expressions, and custom validation logic.

  • Object Structure: JSON Schema allows you to specify the structure of JSON objects, including required and optional properties, nested objects, and property dependencies.

  • Array Structure: JSON Schema provides rules for defining the structure of JSON arrays, such as minimum and maximum item counts, unique items, and item validation schemas.

  • Metadata and Documentation: JSON Schema supports additional metadata annotations to describe the purpose, usage, and documentation of JSON data, such as titles, descriptions, examples, and links to external resources.

JSON Schema is commonly used in various scenarios, including:

  • Data Validation: JSON Schema allows you to automatically validate JSON data against predefined rules, ensuring that it conforms to the expected structure and constraints. Validation can be performed during data input, processing, or exchange between systems.

  • API Development: JSON Schema can be used to define the structure of API request and response payloads, enabling automatic validation of incoming data and generating API documentation.

  • Data Modeling and Documentation: JSON Schema serves as a formal specification for JSON data models, facilitating communication and collaboration between developers, data analysts, and stakeholders. It provides a clear and standardized way to document the structure and constraints of JSON data.

  • Data Interoperability: JSON Schema promotes interoperability between systems by providing a common vocabulary for describing JSON data structures. It allows systems to exchange data reliably and accurately, even when developed independently.

Overall, JSON Schema is a powerful tool for ensuring the integrity, consistency, and compatibility of JSON data across different systems and use cases. It enables developers to define and enforce rules for data validation, documentation, and interoperability, contributing to better data quality and system reliability.

Creating a JSON Schema

Creating a JSON Schema involves defining the structure, properties, and constraints of a JSON document using the JSON Schema vocabulary. You can create a JSON Schema manually by writing a JSON document that describes the expected format and rules for the JSON data. Here's a step-by-step guide to making a simple JSON Schema:

  • Identify the Structure: Determine the overall structure of the JSON data you want to describe. This includes identifying the top-level properties, nested objects, and arrays within the JSON document.

  • Define the Schema: Write a JSON document that serves as the schema definition. This document will specify the properties, data types, and validation rules for each part of the JSON data.

  • Use JSON Schema Keywords: Utilize JSON Schema keywords to define the schema constraints. Some common keywords include type, properties, required, minimum, maximum, pattern, enum, items, additionalProperties, etc. These keywords allow you to specify data types, property names, required fields, validation rules, and more.

Example: Let's create a JSON Schema for a simple person object with properties for name, age, and email.

json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer",
      "minimum": 0
    },
    "email": {
      "type": "string",
      "format": "email"
    }
  },
  "required": ["name"]
}

In this example:

  • The $schema keyword specifies the JSON Schema version being used.
  • The type keyword specifies that the JSON document must be an object.
  • The properties keyword defines the properties of the object (name, age, email), and their corresponding data types (string, integer).
  • The required keyword specifies that the name property is required.
  • Additional constraints are added for the age property (minimum value of 0) and the email property (using the format keyword to specify that it should adhere to the email format).

Complex types

In JSON Schema, you can declare complex types such as nested objects and arrays by utilizing the object and array keywords, along with other relevant schema keywords for specifying properties, items, and constraints. Here's how you can declare complex types in JSON Schema:

Nested Objects:

You can define nested objects by specifying properties within an object schema.
Here's an example:

json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "person": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
      },
      "required": ["name"]
    }
  }
}

In this example, the schema defines an object with a property named "person", which itself is an object with properties "name" and "age". The "name" property is required.

Arrays:

You can define arrays using the array keyword. You can also specify the schema of items within the array using the items keyword.
Here's an example:

json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "students": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "age": { "type": "integer" }
        },
        "required": ["name"]
      }
    }
  }
}

In this example, the schema defines an object with a property named "students", which is an array of objects. Each object within the array has properties "name" and "age", where "name" is required.

Combining Types:

You can also combine different types of properties within an object schema, including nested objects and arrays.
Here's an example:

json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "person": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "age": { "type": "integer" }
      },
      "required": ["name"]
    },
    "scores": {
      "type": "array",
      "items": {
        "type": "integer"
      }
    }
  }
}

In this example, the schema defines an object with two properties: "person" (a nested object with "name" and "age" properties) and "scores" (an array of integers).

By combining nested objects, arrays, and other JSON Schema keywords like required, minimum, maximum, etc., you can define complex data structures and constraints that accurately describe your JSON data model.