JSON Schema: The Secret to Building Scalable and Maintainable Data Models

Roman Glushach
4 min readJun 21, 2023

--

JSON Schema

As data becomes increasingly important in today’s digital age, it is crucial to have a data model that is both scalable and maintainable. JSON Schema is a declarative language that allows you to annotate and validate JSON documents. JSON Schema enables the confident and reliable use of the JSON data format.

What is JSON Schema?

JSON (JavaScript Object Notation) is a simple and lightweight text-based data format. It is widely used for exchanging data between web browsers and servers, as well as for storing and processing data in various applications.

JSON has many advantages, such as being easy to read and write, being supported by many languages and tools, and being flexible and expressive. However, JSON also has some drawbacks, such as being ambiguous, unstructured, and prone to errors.

For example, consider the following JSON document that represents a product in a catalog:

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

This document is valid JSON, but it does not tell us much about the meaning and constraints of the data. For instance:

  • What is productId? Is it a number or a string? Is it unique or not?
  • Is productName required? Can it be empty or null?
  • Can price be zero or negative? What is the currency?
  • Are all tags strings? Can there be duplicates?

These are some of the questions that JSON Schema can help us answer. JSON Schema is an IETF standard that provides a format for describing the structure, constraints, and semantics of JSON data. JSON Schema enables us to:

  • describe our existing data format in a clear and machine-readable way
  • provide documentation for ourselves and others who use our data
  • validate our data against the schema to ensure its quality and correctness
  • reuse common definitions and avoid duplication in our data
  • evolve our data format over time without breaking existing applications

JSON Schema is not a programming language, but rather a specification that defines a set of keywords and rules for writing schemas. A schema is a JSON document that describes another JSON document. A schema can be applied to a whole document or a part of it.

A schema can also be identified by a URI, which can be used to reference it from other schemas or documents. A schema can also contain annotations, such as title and description, that provide human-readable information about the schema or the data.

There are quite a lot of tools and libraries for JSON schema validation you can chose from.

Python:

  • jsonschema

JavaScript:

  • joi by hapijs
  • ajv by ajv-validator

TypeScript:

  • zod by colinhacks

In general, all JSON schema validation implementations are based on the standards and guidelines set out at https://json-schema.org/

Benefits of using JSON Schema

JSON Schema has many benefits for building scalable and maintainable data models, such as:

  • improving the clarity and consistency of your data format
  • reducing the risk of errors and bugs in your data processing
  • enhancing the communication and collaboration between different parties who use your data
  • provides clear human- and machine-readable documentation
  • enabling the automation of tasks such as testing, documentation, code generation, etc.

CloudEvents specifications

CloudEvents is a specification for describing event data in a common way, ensuring that teams have consistency in their applications, helping to reduce some of the errors traditionally faced and simplifying integrations. It is a Cloud Native Computing Foundation (CNCF) incubating project that provides a set of metadata, called attributes, about the event being transferred between systems, and how those pieces of metadata should appear in that message. CloudEvents is organized by the CNCF’s Serverless Working Group.

CloudEvents is designed to be extensible, meaning that it can be used with different protocols and message formats. The specification defines a set of required and optional attributes that can be used to describe an event. The required attributes include id, source, specversion, and type, while the optional attributes include data, datacontenttype, dataschema, subject, and time.

CloudEvents supports different message formats, including JSON, XML, and binary formats. The JSON implementation of CloudEvents is the most commonly used format, and all CloudEvents implementations must support it. The JSON format for CloudEvents is defined by the CloudEvents JSON Schema.

Conclusion

JSON Schema is a powerful tool for building scalable and maintainable data models. It provides a way to define the structure of the data, which can help ensure that the data is consistent and conforms to the expected format. JSON Schema can be used in a variety of contexts, such as when designing APIs or when working with document databases. By using JSON Schema, you can ensure that your data is reliable, consistent, and easy to manage.

--

--

Roman Glushach
Roman Glushach

Written by Roman Glushach

Senior Software Architect & Engineer Manager at Freelance

No responses yet