Skip to main content

Introduction to JSON Schema

Click here if YouTube does not work go to our GitHub.

We will now briefly explain Schema Languages and then move on to JSON Schema. JSON Schema is particularly important in the Web of Things context as it describes and validates JSON data.

Schema

Many payload formats offer a way to describe what a payload instance should look like. Generally, these are called Schema Languages.

For example:

  • JSON Schema
  • XML Schema
  • PDF Schema

JSON Schema

JSON Schema serves as a JSON document that describes and validates the structure of a JSON payload instance.

Its main purpose is to provide guidance to senders, such as clients, on the expected format of their requests, while enabling receivers to automatically validate incoming payloads. Despite being JSON itself, JSON Schema serves as metadata.

json schema diagram

For instance, a basic JSON Schema like "type": "array" specifies that it accepts an array type in other JSON documents. Similar specifications can be defined for other data types in JSON, such as:

  • string
  • number
  • boolean
  • null

During the validation process on the receiver's end, this schema ensures that the incoming data conforms to the specified structure. Below is an example of how such validation can be implemented in JavaScript:

js-schema

It can look different in other programming languages. For instance in Python:

validate(instance={"name" : "Eggs", "price" : 34.99}, schema=schema)

Java:

Set<ValidationMessage> errors = schema.validate(node);

C++:

jsonschema::json_validator<json> validator(sch);
// Will call reporter for each schema violation
validator.validate(data, reporter);

Of course, way more advanced validations can be possible with JSON Schema such as:

  • Length of a String
  • Regular expressions
  • URIs
  • Email addresses
  • IP addresses
  • Max/min for numbers

In Arrays:

  • How many items
  • Allowed types

In Objects:

  • Required properties
  • Amount of properties