JSON Schema allows you to define a strict structure for data transmitted between communicating applications.
On this page
Schema
A JSON object does not include any context or metadata, and there is no way to know by just looking at the object what the properties mean or what the allowed values are.
Below you have an object that belongs to a product catalog:
{ "id": 65, "name": "A green door", "price": 12.50, "tags": [ "home", "green" ]}A schema is a JSON document that contains the description and restrictions that a JSON document must have to be compliant with that schema and, therefore, an instance of that schema.
Create a procut.json file.
The most basic schema is an empty JSON object, which restricts nothing, allows nothing, and describes nothing:
{}By adding validation properties to the schema, you can apply restrictions to an instance.
For example, you can use the "type" property to restrict an instance to an object, array, string, number, boolean, or null:
{ "type": "string" }To create a basic schema definition, define the following properties:
$schema: specifies which “draft” of the JSON Schema standard the schema adheres to.$id: sets a URI for the schema. You can use this unique URI to reference schema elements from within the same document or from external JSON documents.titleanddescription: indicate the intent of the schema. These properties do not add any restrictions to the data being validated.type: defines the first restriction of the JSON data. In the product catalog example below, this property specifies that the data must be a JSON object.
For example:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://example.com/product.schema.json", "title": "Product", "description": "A product in the catalog", "type": "object"}Properties
properties is a validation property that consists of an object where each property represents a property of the JSON data being validated.
You can also specify which properties defined in the object are required.
Using the product catalog example, id is a numeric value that uniquely identifies a product. Since this is the product’s canonical identifier, it is required.
Add the properties validation property to the schema:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "https://example.com/product.schema.json", "title": "Product", "type": "object", "properties": {}}Add the id property along with the following annotations:
description: indicates thatidis the product’s unique identifier.typedefines the type of data that is valid, in this case the product identifier must be an integer.
{ "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer" } }}Add the name property of type string:
{ "type": "object", "properties": { "id": { "description": "The unique identifier for a product", "type": "integer" }, "name": { "description": "Name of the product", "type": "string" } }}Continue reading: https://json-schema.org/learn/getting-started-step-by-step