Panduan referensi lengkap untuk kata kunci dan sintaks JSON Schema
$schemaCore / MetaBerlaku untuk: rootDeclares the JSON Schema dialect version
{ "$schema": "https://json-schema.org/draft/2020-12/schema" }โน Always include at root to declare the dialect
$idCore / MetaBerlaku untuk: anySets the base URI for the schema
{ "$id": "https://example.com/schemas/user.json" }titleCore / MetaBerlaku untuk: anyHuman-readable title for the schema or property
{ "title": "User", "type": "object" }descriptionCore / MetaBerlaku untuk: anyHuman-readable description of the schema's purpose
{ "description": "A registered user account", "type": "object" }$commentCore / MetaBerlaku untuk: anyDeveloper comment โ not rendered to users
{ "$comment": "Added 2024-01 to support OAuth users" }$defs / definitionsCore / MetaBerlaku untuk: rootRepository for reusable sub-schemas
{ "$defs": { "email": { "type": "string", "format": "email" } } }โน $defs is draft 2020-12+; use definitions in draft-07
$refCore / MetaBerlaku untuk: anyReference to another schema by URI or JSON Pointer
{ "$ref": "#/$defs/email" }โน Can reference internal (#/$defs/โฆ) or external schemas
type: stringTypesBerlaku untuk: stringValue must be a JSON string
{ "type": "string" }type: numberTypesBerlaku untuk: numberValue must be a JSON number (integer or float)
{ "type": "number" }type: integerTypesBerlaku untuk: numberValue must be a whole number (no fractional part)
{ "type": "integer" }type: booleanTypesBerlaku untuk: booleanValue must be true or false
{ "type": "boolean" }type: nullTypesBerlaku untuk: nullValue must be JSON null
{ "type": "null" }type: arrayTypesBerlaku untuk: arrayValue must be a JSON array
{ "type": "array" }type: objectTypesBerlaku untuk: objectValue must be a JSON object
{ "type": "object" }type: [multiple]TypesBerlaku untuk: anyValue may be one of the listed types
{ "type": ["string", "null"] }โน Useful for optional/nullable fields
minLength / maxLengthStringsBerlaku untuk: stringMinimum and maximum number of Unicode code points
{ "type": "string", "minLength": 1, "maxLength": 100 }patternStringsBerlaku untuk: stringValue must match a ECMA 262 regular expression
{ "type": "string", "pattern": "^[a-z0-9_]+$" }โน Partial match โ use ^ and $ to anchor
formatStringsBerlaku untuk: stringSemantic validation hint (implementation-dependent)
{ "type": "string", "format": "email" }โน Common values: email, uri, uuid, date, date-time, ipv4, ipv6, hostname
enumStringsBerlaku untuk: anyValue must be one of the listed values
{ "type": "string", "enum": ["active", "inactive", "pending"] }constStringsBerlaku untuk: anyValue must exactly equal the given value
{ "const": "v1" }โน Equivalent to enum with a single value
minimum / maximumNumbersBerlaku untuk: numberInclusive lower and upper bounds
{ "type": "number", "minimum": 0, "maximum": 100 }exclusiveMinimum / exclusiveMaximumNumbersBerlaku untuk: numberExclusive lower and upper bounds (value < max, value > min)
{ "type": "number", "exclusiveMinimum": 0 }โน In draft-07 these were booleans; draft 2019+ they are numbers
multipleOfNumbersBerlaku untuk: numberValue must be a multiple of the given number
{ "type": "integer", "multipleOf": 5 }propertiesObjectsBerlaku untuk: objectSchema for each named property
{ "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" } } }requiredObjectsBerlaku untuk: objectArray of property names that must be present
{ "type": "object", "required": ["id", "email"] }additionalPropertiesObjectsBerlaku untuk: objectSchema or boolean controlling unknown properties
{ "type": "object", "additionalProperties": false }โน false = no extra properties allowed; a schema validates extra properties
patternPropertiesObjectsBerlaku untuk: objectSchemas applied to properties whose name matches the regex key
{ "patternProperties": { "^S_": { "type": "string" } } }minProperties / maxPropertiesObjectsBerlaku untuk: objectMin/max number of properties in the object
{ "type": "object", "minProperties": 1, "maxProperties": 10 }items (draft-07)ArraysBerlaku untuk: arraySchema for all items (or tuple schemas as array)
{ "type": "array", "items": { "type": "string" } }โน In draft-2020-12, items applies to all remaining items after prefixItems
prefixItems (2020-12)ArraysBerlaku untuk: arrayPer-index schemas for a tuple
{ "type": "array", "prefixItems": [{ "type": "string" }, { "type": "number" }] }โน Replaces tuple form of items from draft-07
minItems / maxItemsArraysBerlaku untuk: arrayMin/max number of array elements
{ "type": "array", "minItems": 1, "maxItems": 50 }uniqueItemsArraysBerlaku untuk: arrayAll items must be unique
{ "type": "array", "items": { "type": "string" }, "uniqueItems": true }containsArraysBerlaku untuk: arrayAt least one item must match the given schema
{ "type": "array", "contains": { "const": "admin" } }โน Use minContains / maxContains to control how many must match
allOfCompositionBerlaku untuk: anyValue must be valid against ALL listed schemas
{ "allOf": [{ "type": "string" }, { "minLength": 5 }] }โน Intersection โ use for schema composition/extension
anyOfCompositionBerlaku untuk: anyValue must be valid against AT LEAST ONE listed schema
{ "anyOf": [{ "type": "string" }, { "type": "number" }] }oneOfCompositionBerlaku untuk: anyValue must be valid against EXACTLY ONE listed schema
{ "oneOf": [{ "type": "string" }, { "type": "number" }] }โน Validates all and ensures exactly one matches โ slower than anyOf
notCompositionBerlaku untuk: anyValue must NOT be valid against the given schema
{ "not": { "type": "null" } }if / then / elseCompositionBerlaku untuk: anyConditional validation: if condition matches, then/else applies
{ "if": { "properties": { "type": { "const": "email" } } }, "then": { "required": ["email"] }, "else": { "required": ["phone"] } }{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/user.json",
"title": "User",
"description": "A registered user account",
"type": "object",
"required": ["id", "email", "role"],
"properties": {
"id": {
"type": "integer",
"minimum": 1
},
"email": {
"type": "string",
"format": "email",
"maxLength": 255
},
"name": {
"type": ["string", "null"],
"maxLength": 100
},
"role": {
"type": "string",
"enum": ["admin", "editor", "viewer"]
},
"tags": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true,
"maxItems": 20
},
"createdAt": {
"type": "string",
"format": "date-time"
}
},
"additionalProperties": false
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"address": {
"type": "object",
"required": ["street", "city", "country"],
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"zip": { "type": "string", "pattern": "^[0-9]{5}$" },
"country":{ "type": "string", "minLength": 2, "maxLength": 2 }
}
}
},
"type": "object",
"properties": {
"billing": { "$ref": "#/$defs/address" },
"shipping": { "$ref": "#/$defs/address" }
}
}Kata kunci JSON Schema adalah properti standar untuk mendefinisikan tipe data, format, dan batasan, seperti $ref, properties, required, dan type.
Kata kunci $ref memungkinkan Anda mereferensikan definisi schema lain dan membuat komponen schema yang dapat digunakan kembali.
properties mendefinisikan properti objek, sedangkan required adalah array yang menentukan properti mana yang wajib ada.
JSON Schema telah berkembang melalui beberapa versi (Draft-04, Draft-07, 2019-09, 2020-12), dengan setiap versi menambahkan kata kunci dan fitur baru.