Jump to content

Draft:JSON Schema

From Wikipedia, the free encyclopedia


JSON Schema
Filename extension
.schema.json, .json
Internet media type
application/schema+json
Developed byJSON Schema Working Group
Type of formatData interchange format
Extended fromJSON
StandardDraft 2020-12
Open format?Yes
Websitejson-schema.org

JSON Schema is a specification for defining the structure and constraints of JSON (JavaScript Object Notation) data. It allows for validation, documentation, and interaction control of JSON-based formats, making it widely used in API development, data validation, and configuration management.[1]

Overview

[edit]

JSON Schema provides a declarative way to describe JSON data. It enables validation by defining expected properties, types, formats, and constraints. JSON Schema is widely used to enforce consistency in data structures and improve interoperability between systems.[2]

Example

[edit]

A sample JSON document and its schema:

JSON Data:

{
  "productId": 1,
  "productName": "A green door",
  "price": 12.50,
  "tags": ["home", "green"]
}

JSON Schema:

{
  "$schema": "http://json-schema.org/draft/2020-12/schema",
  "title": "Product",
  "type": "object",
  "properties": {
    "productId": { "type": "integer" },
    "productName": { "type": "string" },
    "price": { "type": "number", "minimum": 0 },
    "tags": { "type": "array", "items": { "type": "string" } }
  },
  "required": ["productId", "productName", "price"]
}

Features

[edit]

Data Validation

[edit]

JSON Schema validates data against predefined rules:

{
"type": "string",
"minLength": 5,
"pattern": "^[A-Z][a-z]+$"
}

This schema ensures a string starts with a capital letter, followed by lowercase letters, with minimum length of 5 characters.

Documentation

[edit]

JSON Schema provides self-documenting metadata:

{
  "title": "User",
  "description": "A user account in the system",
  "properties": {
    "username": {
      "description": "Unique identifier for the user"
    }
  }
}

The title, description, and property descriptions help developers understand the data structure.

Hypermedia Support

[edit]

JSON Hyper-Schema extends JSON Schema for hypermedia-driven APIs:

{
  "links": [
    {
      "rel": "self",
      "href": "/users/{id}",
      "templateRequired": ["id"]
    }
  ]
}

This defines a link relation that constructs URLs using instance data, enabling HATEOAS principles.

Interoperability

[edit]

JSON Schema works across different programming languages.

A schema could be emitted by a library in one programming language, and used for validation in a different programming language.

Versions

[edit]

JSON Schema has undergone multiple revisions:

  • Draft 4 (2013) – First widely adopted version.
  • Draft 6 (2017) – Introduced new keywords and enhanced validation.
  • Draft 7 (2018) – Added support for complex validation scenarios.
  • Draft 2019-09 (2019) – Introduced dynamic references and new annotation features.
  • Draft 2020-12 (2020) – The latest version, refining features and improving interoperability.[3]

Implementations

[edit]

JSON Schema is supported in various programming languages:

  • Python: The 'jsonschema' library provides validation capabilities.[4]
  • JavaScript: The Ajv library offers efficient JSON Schema validation.[5]
  • Java: Libraries such as Everit JSON Schema enable schema validation in Java applications.[6]

Applications

[edit]

JSON Schema is commonly used in:

  • API Development: Ensures consistency in request and response formats.
  • Configuration Management: Validates configuration files for correctness.
  • Data Storage: Helps define structured data models in NoSQL databases.[7]

Tools

[edit]

Several tools facilitate working with JSON Schema:

  • JSON Schema Validator: Online tool for validation.[8]
  • JSON Builder: Interactive tool for creating and testing JSON structures.[9]

See also

[edit]

References

[edit]
  1. ^ "JSON Schema". json-schema.org. Retrieved 2025-03-30.
  2. ^ "What is a schema?". json-schema.org. Retrieved 2025-03-30.
  3. ^ "JSON Schema Specification". json-schema.org. Retrieved 2025-03-30.
  4. ^ "jsonschema". Python Package Index. Retrieved 2025-03-30.
  5. ^ "Ajv JSON Schema Validator". Retrieved 2025-03-30.
  6. ^ "Everit JSON Schema". GitHub. Retrieved 2025-03-30.
  7. ^ "JSON Schema Examples". MongoDB. Retrieved 2025-03-30.
  8. ^ "JSON Schema Validator". Newtonsoft. Retrieved 2025-03-30.
  9. ^ "JSON Builder". Retrieved 2025-03-30.
[edit]