Fundamentals 5 min read

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.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Practical Guide to JSON Schema for Data Validation

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
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.
JSONschemadata validationAPI testingjsonschema
360 Quality & Efficiency
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.