Overview

What is JSON Schema?

JSON Schema is a declarative language that you can use to annotate and validate the structure, constraints, and data types of your JSON documents. It provides a way to standardize and define expectations for your JSON data.

How JSON Schema works

How it works

Using JSON Schema, you can define rules and constraints that JSON data should adhere to. When your JSON documents adhere to these constraints, it becomes easier to exchange structured data between applications because the data follows a consistent pattern.

Before we get into JSON Schema and how it can help us, let's first understand what exactly is a JSON document.

  • A JSON document represents a piece of data that follows the syntax and structure defined by the JSON format. It is a collection of key-value pairs, arrays, and nested objects.
  • JSON documents are used to store and transfer data between systems and applications.

Taking an example of a JSON document representing a customer order:

data
{ "order_id": "123456", "customer_name": "John Doe", "items": [ { "product_id": "P001", "name": "T-shirt", "quantity": 2, "price": 19.99 }, { "product_id": "P002", "name": "Jeans", "quantity": 1, "price": 49.99 } ], "total_amount": 89.97, "status": "pending"}
  • The above code snippet includes attributes such as the order ID, customer name, items ordered (an array of objects with product details), shipping address, total amount, and status of the order.

  • This JSON document provides a structured representation of an order, making it easy to exchange, store, or process the order information in various applications or systems.

The challenge

When working with JSON data, it can quickly become complex and difficult to manage, especially when dealing with nested structures. Without a standardized schema, it becomes challenging to validate and enforce constraints on the data.

For example, if we wanted to validate JSON data using Python:

1# Without JSON Schema
2data = {
3    "product": {
4        "name": "Widget",
5        "price": 10.99,
6        "quantity": 5
7    }
8}
9
10# Performing basic validation
11if "product" in data and isinstance(data["product"], dict) and "name" in data["product"] and "price" in data["product"]:
12    print("Valid JSON object.")
13else:
14    print("Invalid JSON object.")

In the above code snippet, we are performing basic validation to check if the JSON object has the required fields. Since this is a relatively simpler data, this way of checking works for now.

To show the challenges of performing data validation without using JSON Schema, we can take this example in Python:

1# Without JSON Schema
2data = {
3    "order": {
4        "order_id": "123456",
5        "customer_name": "John Doe",
6        "items": [
7            {
8                "product_id": "P001",
9                "name": "T-shirt",
10                "quantity": 2,
11                "price": 19.99
12            },
13            {
14                "product_id": "P002",
15                "name": "Jeans",
16                "quantity": 1,
17                "price": 49.99
18            }
19        ],
20        "shipping_address": {
21            "street": "123 Main St",
22            "city": "New York",
23            "state": "NY",
24            "postal_code": "10001"
25        },
26        "total_amount": 89.97,
27        "status": "pending"
28    }
29}
30
31# Performing basic validation
32if (
33    "order" in data
34    and isinstance(data["order"], dict)
35    and "order_id" in data["order"]
36    and "customer_name" in data["order"]
37    and "items" in data["order"] and isinstance(data["order"]["items"], list)
38    and "shipping_address" in data["order"] and isinstance(data["order"]["shipping_address"], dict)
39    and "total_amount" in data["order"]
40    and "status" in data["order"]
41):
42    print("Valid JSON object.")
43else:
44    print("Invalid JSON object.")

Now we are dealing with a complex JSON structure that represents an order. The basic validation logic checks whether the required fields exist in the JSON object. However, as the structure becomes more complex, the validation code becomes more complicated and prone to errors. Moreover, this approach lacks support for checking data types, handling nested structures, and enforcing specific constraints.

JSON Schema to the rescue

JSON Schema provides a solution to this problem. It is a specification language for JSON that allows you to describe the structure, content, and semantics of a JSON instance. With JSON Schema, you can define metadata about an object's properties, specify whether fields are optional or required, and define expected data formats.

By using JSON Schema, people can better understand the structure and constraints of the JSON data they are using. It enables applications to validate data, ensuring it meets the defined criteria. With JSON Schema, you can make your JSON more readable, enforce data validation, and improve interoperability across different programming languages.

Using the same example, we can validate the data by making use of the jsonschema Python library:

1from jsonschema import validate
2
3# Using JSON Schema
4data = {
5    "product": {
6        "name": "Widget",
7        "price": 10.99,
8        "quantity": 5
9    }
10}
11
12schema = {
13    "$schema": "https://json-schema.org/draft/2020-12/schema",
14    "title": "Product",
15    "type": "object",
16    "properties": {
17        "product": {
18            "type": "object",
19            "properties": {
20                "name": {
21                    "type": "string"
22                    },
23                "price": {
24                    "type": "number", 
25                    "minimum": 0
26                    },
27                "quantity": {
28                    "type": "integer", 
29                    "minimum": 1
30                    }
31            },
32            "required": ["name", "price", "quantity"]
33        }
34    },
35    "required": ["product"]
36}
37
38try:
39    validate(data, schema)
40    print("Valid JSON object.")
41except Exception as e:
42    print("Invalid JSON object:", e)

In the above code snippet, we defined a JSON Schema that describes the expected structure and constraints of the JSON data. We use the jsonschema library to validate the data against the schema. If the data doesn't conform to the schema, an exception is raised, providing detailed information about the failure.

By using JSON Schema, we can easily define and enforce constraints, making the validation process more robust and manageable. It improves the readability of the code and reduces the chances of data-related issues.

Why developers use JSON Schema

With JSON Schema you can:

  • Describe existing data formats: JSON Schema allows you to describe the structure, constraints, and data types of your existing JSON data formats.
  • Define rules and constraints: When your JSON documents adhere to these constraints, it becomes easier to exchange structured data between applications because the data follows a consistent pattern.
  • Clear and readable documentation: JSON Schema supports the creation of documentation that is easily understandable by both humans and machines.
  • Highly extensible and can be tailored to fit your needs.
    • You can create custom keywords, formats, and validation rules to suit your own requirements.
  • Validate your data, which helps you:
    • Automate testing: JSON Schema validation enables automated testing, ensuring that data consistently adheres to the specified rules and constraints.
    • Enhance data quality: By enforcing validation rules, JSON Schema helps ensure the quality of client-submitted data, preventing inconsistencies, errors, and malicious inputs.
  • Wide range of tools availability: The JSON Schema community has a wealth of tools and resources available across many programming languages to help you create, validate, and integrate your schemas.

Why organizations adopt JSON Schema

  • Streamline testing and validation: Simplify your validation logic to reduce your code’s complexity and save time on development. Define constraints for your data structures to catch and prevent errors, inconsistencies, and invalid data.
  • Exchange data seamlessly: Establish a common language for data exchange, no matter the scale or complexity of your project. Define precise validation rules for your data structures to create shared understanding and increase interoperability across different systems and platforms.
  • Document your data: Create a clear, standardized representation of your data to improve understanding and collaboration among developers, stakeholders, and collaborators.
  • Vibrant tooling ecosystem: Adopt JSON Schema with an expansive range of community-driven tools, libraries, and frameworks across many programming languages.

JSON Schema History

JSON Schema has a rich history that dates back to the first JSON Schema proposal submitted by Kris Zyp to json.com on October 2nd, 2007.

The current version of JSON Schema is 2020-12, which represents the latest advancements and have expended capabilities as compared with the previous version draft-04, draft-06, draft-07. We encourage everyone to adopt the latest version whenever possible to take advantage of all the advancements and benefits of JSON Schema.

For more information regarding JSON Schema history, you can refer to this article by Henry Andrews.

Next steps

To start using JSON Schema, see Creating your first schema.

Learn more

Learn more about the specification:

Join the community

To get involved with our community, please make sure you are familiar with the project's Code of Conduct.

  • Join JSON Schema Slack. This is the best place to ask questions, learn, get help, or discuss all things JSON Schema.
  • Attend our public JSON Schema meetings. We hold Office Hours every first Tuesday at 15:00 BST (and by appointment) and Open Community Working Meetings every Monday at 14:00 PT.
  • Follow our YouTube channel. Find recordings of our public community meetings and JSON Schema learning resources.
  • Read our blog. Find the latest and greatest stories from our community.

Need Help?

Did you find these docs helpful?

Help us make our docs great!

At JSON Schema, we value docs contributions as much as every other type of contribution!

Still Need Help?

Learning JSON Schema is often confusing, but don't worry, we are here to help!.