Practical Guide to JSON Schema for Data Validation
This article explains the purpose, features, and practical usage of JSON Schema for describing data structures, generating documentation, and validating API inputs and outputs, and provides a detailed example with code and tips for effective schema design.
JSON Schema is a standard for defining constraints on JSON data, enabling both parties in data exchange to understand and validate the required format, thus ensuring correct data transmission.
Key uses of JSON Schema include describing complex data structures, generating self‑describing documentation, and performing data validation by specifying required fields, types, and other constraints.
In automated testing, JSON Schema can validate API responses; an example schema is shown below, illustrating object and array nesting, property definitions, and numeric constraints.
schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "some information about this",
"type": "object",
"required": [
"sdkcorev", "price"
],
"properties": {
"sdkcorev": {
"type": "string",
"minLength": 3,
"maxLength": 30
},
"price": {
"type": "number",
"multipleOf": 0.5, # 能够被0.5整除
# 这里没有取等,5.0<price<99999.0
"minimum": 5.0,
"maximum": 99999.0,
# 若使用下面这两个关键字则 5.0<=price<=99999.0
# "exclusiveMinimum": 5.0,
# "exclusiveMaximum": 99999.0
},
"tags": {
"type": "array",
"items": [
{
"type": "string",
"minLength": 2,
"maxLength": 8
},
],
"additonalItems": {
"type": "string",
"miniLength": 2
},
"miniItems": 1,
"maxItems": 5,
"uniqueItems": True
},
"coding": {
"type": "string",
"pattern": "^[A-Z]+[a-zA-Z0-9]{12}$"
},
"other": {
"type": "object",
"properties": {
"info1": {
"type": "string"
},
}
}
},
"minProperties": 3, # 最小属性个数
"maxProperties": 7, # 最大属性个数
}The schema validates the JSON fields returned by an interface, checking types, lengths, patterns, and numeric ranges.
Structure syntax guidelines include avoiding underscores in field names, using "$schema" to indicate the draft version, providing a description, and specifying the "type" (object, array, string, integer, number, boolean, enum, null). For objects, key keywords are "properties", "required", and "additionalProperties"; for arrays, they are "items", "minItems", and "uniqueItems"; for strings, they are "maxLength", "minLength", and "pattern"; for numbers, they are "minimum", "maximum", and exclusive variants.
Usage tips suggest reusing schemas when only a subset of fields needs validation, adding required fields only for those that must be present, which reduces repetitive schema definitions.
In summary, JSON Schema is primarily used to constrain and validate data, describe known data formats, ensure data quality, and reduce manual validation effort.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
