Summary of JSON: Data Interchange and Schema
JSON: Data Interchange and Schema for Students
Introduction
JSON Schema is a language used to describe the structure of JSON documents and to validate them. It allows you to define expected data types, required properties, number ranges, string formats, and relationships between different parts of a document. This material explains the key concepts of JSON Schema (draft 2020-12), providing practical examples and notes for use in real-world projects.
Definition: JSON Schema is a formal specification for describing, validating, and documenting the structure of JSON documents.
Basic Principles
Schema Declaration and Identification
- Each schema can begin with a version declaration:
{ "$schema": "https://json-schema.org/draft/2020-12/schema" } - An identifier can optionally be set: "$id": "http://example.com/schemas/myschema.json". If not specified, it is an anonymous schema.
Annotations and Comments
- Properties like title, description, and $comment serve for documentation and are not validation rules.
Definition: Annotations are descriptive fields within a schema (e.g., title, description) that help understand the data but do not affect validation.
Data Types and Constraints
JSON Schema is built upon native JSON types: object, array, string, number, integer, boolean, null.
Numbers and Integers
- Types: integer, number.
- Constraints: multipleOf, minimum, exclusiveMinimum, maximum, exclusiveMaximum.
Example: the value must be a multiple of 10 and greater than or equal to 0
{ "type": "number", "multipleOf": 10, "minimum": 0 }
Strings
- Constraints: minLength, maxLength, pattern (RegExp (ECMA-262 standard)), format (e.g., date-time, email, uri, uuid).
Example: email and length
{ "type": "string", "format": "email", "maxLength": 254 }
Booleans and Null
- type": "boolean" for true/false
- type": "null" explicitly represents a null value (not a missing value)
Enum
- Defines specific allowed values: enum: ["red","amber","green"]
Working with Objects
Basic Object Definition
- Type: type: "object"
- Internal properties are defined using properties.
- By default, properties not specified in
propertiesare allowed.
Example:
{
"type": "object",
"properties": {
"productId": { "type": "integer" },
"productName": { "type": "string" }
},
"required": ["productId"]
}
Definition:
requiredis an array of property names that must be present in the object for validation.
additionalProperties and patternProperties
- additionalProperties: true/false or a schema object. By default, it is true (allowing unspecified properties).
- E.g.,
"additionalProperties": { "type": "string" }allows additional properties, but they must be strings.
- E.g.,
- patternProperties: maps regular expressions to schemas for property names.
- Example:
"patternProperties": { "^S_": { "type": "string" } }applies the rule to all properties starting with S_.
- Example:
propertyNames and Property Count Constraints
- propertyNames allows validating the property names themselves using a pattern, e.g.,
"pattern": "^[A-Za-z0-9]*$". - minProperties, maxProperties define the allowed number of properties in an object.
Unevaluated properties
- unevaluatedProperties functions similarly to
additionalProperties, but it fits into situations involving schema composition (extending closed schemas). It is used in combinations where some properties have already been evaluated by other subschemas.
Table: Comparison of Properties for Unexpected Keys
| Key | Behavior | Note |
|---|---|---|
| additionalProperties | Allow/disallow or define schema | Applies to unspecified properties |
| patternProperties | Conditional by name (RegExp) | Controlled by property name |
| unevaluatedProperties | Applies after subschema evaluation | Useful in composition |
Arrays
Basic Properties
- type: "array", **i
Already have an account? Sign in
JSON Schema Overview
Klíčové pojmy: Declare the version using "$schema" and the identifier using "$id"., Use 'properties' and 'required' to define an object's mandatory properties., By default, 'additionalProperties' allows unknown properties; set it to 'false' for a closed object., 'patternProperties' and 'propertyNames' allow you to validate property names using regular expressions., 'prefixItems' for tuple validation; 'items' or 'items: false' for additional items., 'contains' with 'minContains'/'maxContains' checks the number of items that meet a condition., Use 'allOf'/'anyOf'/'oneOf'/'not' to compose complex rules., References via '$ref' and '$defs' enable schema reuse and division., JSON Pointer (RFC 6901) addresses parts of a schema, e.g., #/properties/street_address., Use formats (email, date-time, uri) and 'pattern' for strings., Set 'minItems'/'maxItems' and 'uniqueItems' for arrays as needed., Use 'unevaluatedProperties' carefully when composing subschemas.