Skip to main content

JSON Schema Examples

JSON Schema is an open-standard format for defining the structure of JSON data. In other words, it defines the different elements that are allowed in a given JSON document (also known as a JSON schema), as well as the rules that must be followed when creating and manipulating such documents.

This makes it an extremely useful tool for developers who work with JSON data, as it enables them to create validations that can be used to confirm whether a given document matches the schema's requirements.

Additionally, JSON Schema can also be used to generate documentation about the structure of a given document, which can help developers better understand and work with that data.

Basic JSON Schema Examples

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/student.schema.json",
  "title": "Student",
  "description": "A student from Georgia Institute of Technology",
  "type": "object",
  "properties": {
    "studentId": {
      "description": "The unique identifier for a student",
      "type": "integer"
    }
  },
  "required": [ "studentId" ]
}

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Person",
    "description": "A person schema to represent an individual within a system.",
    "type": "object",
    "properties": {
        "name": {
            "description": "Person's name",
            "type": "string"
        },
        "age": {
            "description": "Age in years which must be equal to or greater than zero.",
            "type": ["integer"],
            "minimum": 0
        }
}
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "$id": "https://example.com/product.schema.json",
    "title": "Record of product",
    "description": "This document records the details of an product",
    "type": "object",
    "properties": {
        "id": {
            "description": "A unique identifier for an product",
            "type": "number"
        },
        "name": {
            "description": "Full name of the product",
            "type": "string"
        },
        "manufactureredYear": {
            "description": "Year that the productw as manufacturered",
            "type": "number"
        },
        "attributes": {
            "description": "Attributes of the product",
            "type": "object",
            "properties": {
                "colors": {
                    "type": "array",
                    "items": {
                        "description": "List of available colors",
                        "type": "string"
                    }
                },
                "material": {
                    "type": "array",
                    "items": {
                        "description": "List of available material",
                        "type": "string"
                    }
                }
            }
        }
    }
}

Uses of JSON Schema

There are many different uses and applications for JSON schema, including data validation, document transformation, and data exchange.

One of the key benefits of using JSON schema is that it provides a standardized format and set of rules for defining the structure and content of your data. This makes it easier to work with large or complex data sets, as well as to ensure that you are storing and representing your data in a consistent and accurate way.

Some of the most common uses of JSON schema include validating user input, transforming or converting data between different formats, and exchanging data between applications or systems. It can also be used to define complex business logic rules for processing or manipulating your data, as well as to create automation workflows or data pipelines.

In particular, it can be used to:

  • Define the structure of JSON documents by specifying rules for different elements within those documents. This enables developers to create validations (also known as "schemas") that determine whether a given JSON document matches the desired structure.
  • Generate documentation for the structure of a JSON schema, which can be used to help developers understand and work with the given data.
  • Apply those schemas in applications like interactive data validation, configuration file generation, etc..