What is JSON Schema

First, JSON Schema is a standard JSON file. Json Schema A JSON data specification described as a JSON string. Json Schema can be used to verify that a given JSON string meets the convention’s JSON data specification.

Let’s take a look at two more examples of what JSON Schema really is

Example 1: The simplest JSON schema is unlimited

Although the purpose of using JSON Schema is to restrict and validate jSON-formatted data, empty objects are fully valid schemas and can accept any valid JSON.

json schema

{}
Copy the code

These are all valid data

31
Copy the code
"I'm a string"
Copy the code
{ "an": [ "arbitrarily"."nested"]."data": "structure" }
Copy the code

Example 2: JSON Schema with a few constraints

How does json schema contract json data

{
	"type": "object"."properties": {
		"number": {
			"type": "number"
		},
		"street_name": {
			"type": "string"
		},
		"street_type": {
			"type": "string"."enum": ["Street"."Avenue"."Boulevard"]}},"required": ["number"."street_name"]}Copy the code
  • The type keyword indicates that the qualified type is object
  • The properties keyword specifies that the object has three properties number,street_name, and street_type
  • The type of each attribute indicates the data type of that attribute
  • Enum keyword indicates that the street_type value can only be “Street”, “Avenue”, or “Boulevard”
  • Required stands for number,street_name is a required property, and by default, properties defined by the properties keyword are not required.

Data that can be verified

{
	"number": 1600."street_name": "Pennsylvania"."street_type": "Avenue"."direction": "NW"
}
Copy the code
{
	"number": 1600."street_name": "Pennsylvania"
}
Copy the code

Failed to verify the data

Missing required attributes

{
	"number": 1600
}
Copy the code

The value “super-speed” used by street_type is not an enumerated value as agreed in the JSON Schema

{
	"number": 1600."street_name": "Pennsylvania"."street_type": "super-speed"
}
Copy the code

Keywords Introduction

Json schema uses keywords to describe restrictions on JSON data. The following describes only a few. For details, see json-schema.org/understandi…

type keyword

Type Indicates the type of the data

  • string
  • Number (integer, floating point number)
  • Integer (integer)
  • object
  • array
  • boolean
  • null

Alternatively, number can be restricted to multiples of a given number using the multipleOf keyword. It can be set to any positive number. In the following example, any multiple of 10 can be checked.

{
    "type"       : "number"."multipleOf" : 10
}
Copy the code

You can qualify the range range attribute of a number

X ≥ minimum x > exclusiveMinimum x ≤ maximum x < exclusiveMaximumCopy the code

example:

{
  "type": "number"."minimum": 0."exclusiveMaximum": 100
}
Copy the code
{
  "type": "number"."minimum": 0."maximum": 100."exclusiveMaximum": true
}
Copy the code

Indicates the data type is string

json schema

{ "type": "string" }
Copy the code

Valid data

"I'm a string"
Copy the code

$schema

Because JSON Schema is JSON itself, it’s hard to tell what is A JSON Schema and what is just JSON data. The $schema keyword is used to declare JSON schema. It is usually a good practice to include it, although it is not required.

You can also use this keyword to declare which version of the JSON schema specification to write the schema to.

{ "$schema": "http://json-schema.org/schema#" }
Copy the code

$id

Declaring a JSON schema includes a $ID attribute as a unique identifier for each schema.

It does two things:

  • In most cases, this URI also indicates where the JSON file containing the pattern should be downloaded.
{ "$id": "http://yourdomain.com/schemas/myschema.json" }
Copy the code
  • It declares a base URI against which the $ref URI reference is resolved.
{ "$ref": "person.json" }
Copy the code

$comment

The $comment keyword is strictly used to add comments to the JSON schema source. Its value must always be a string. Unlike the title, description, and examples of annotations, the JSON schema implementation does not allow you to attach any meaning or behavior to them, and can even remove them at any time.

reference

Json-schema.org/understandi…