JSON - Schema

Advertisements


JSON Schema is a specification for JSON based format for defining structure of JSON data. It was written under IETF draft which expired in 2011. JSON Schema:

  • Describes your existing data format.

  • Clear, human- and machine-readable documentation.

  • Complete structural validation, useful for automated testing.

  • Complete structural validation, validating client-submitted data.

JSON Schema Validation Libraries

There are several validators currently available for different programming languages. Currently the most complete and compliant JSON Schema validator available is JSV

LanguagesLibraries
CWJElement (LGPLv3)
Javajson-schema-validator (LGPLv3)
.NETJson.NET (MIT)
ActionScript 3Frigga (MIT)
Haskellaeson-schema (MIT)
PythonJsonschema
Rubyautoparse (ASL 2.0); ruby-jsonschema (MIT)
PHPphp-json-schema (MIT). json-schema (Berkeley)
JavaScriptOrderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js.

JSON Schema Example

Following is a basic JSON schema which covers a classical product catalog description:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "integer"
        },
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

Let's check various important keywords which can be used in this schema:

KeywordsDescription
$schemaThe $schema keyword states that this schema is written according to the draft v4 specification.
title You will use this to give a title to your schema
descriptionA little description of the schema
typeThe type keyword defines the first constraint on our JSON data: it has to be a JSON Object.
propertiesDefines various keys and their value types, minimum and maximum values to be used in JSON file.
requiredThis keeps a list of required properties.
minimumThis is the constraint to be put on the value and represents minimum acceptable value.
exclusiveMinimumIf "exclusiveMinimum" is present and has boolean value true, the instance is valid if it is strictly greater than the value of "minimum".
maximumThis is the constraint to be put on the value and represents maximum acceptable value.
exclusiveMaximumIf "exclusiveMaximum" is present and has boolean value true, the instance is valid if it is strictly lower than the value of "maximum".
multipleOfA numeric instance is valid against "multipleOf" if the result of the division of the instance by this keyword's value is an integer.
maxLengthThe length of a string instance is defined as the maximum number of its characters.
minLengthThe length of a string instance is defined as the minimum number of its characters.
patternA string instance is considered valid if the regular expression matches the instance successfully.

You can check a http://json-schema.org for complete list of keywords which can be used in defining JSON schema. Above schema can be used to test the validity of the below given JSON code:

[
    {
        "id": 2,
        "name": "An ice sculpture",
        "price": 12.50,
    },
    {
        "id": 3,
        "name": "A blue mouse",
        "price": 25.50,
    }
]


Advertisements
Advertisements