logo

JSON Schema


Show

JSON Schema is particularly for JSON-based format for explaining the structure of JSON data. It was written below the IETF draft which finished in 2011. JSON Schema:

  • Describe your present 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 some validators presently available for numerous programming languages. Presently, the most complete and compliant JSON Schema validator available is JSV.

Languages

Libraries

C

WJElement (LGPLv3)

Java

json-schema-validator (LGPLv3)

.NET

Json.NET (MIT)

ActionScript 3

Frigga (MIT)

Haskell

aeson-schema (MIT)

Python

Jsonschema

Ruby

autoparse (ASL 2.0); ruby-jsonschema (MIT)

PHP

php-json-schema (MIT). json-schema (Berkeley)

JavaScript

Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or AFL 2.0); schema.js.

Example of JSON Schema

Below given example is a primary 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"]
}

Below are the numerous important keywords that can make use in this Schema:

Sr.No.

Keyword & Description

1

$schema

The $schema keyword states that this schema is written according to the draft v4 specification.

2

title

You will use this to give a title to your schema.

3

description

A little description of the schema.

4

type

The type keyword defines the first constraint on our JSON data: it has to be a JSON Object.

5

properties

Defines various keys and their value types, minimum and maximum values to be used in JSON file.

6

required

This keeps a list of required properties.

7

minimum

This is the constraint to be put on the value and represents the minimum acceptable value.

8

exclusiveminimum

If "exclusiveMinimum" is present and has the boolean value true, the instance is valid if it is strictly greater than the value of "minimum".

9

maximum

This is the constraint to be put on the value and represents the maximum acceptable value.

10

exclusiveMaximum

If "exclusiveMaximum" is present and has the boolean value true, the instance is valid if it is strictly lower than the value of "maximum".

11

multipleOf

A numeric instance is valid against "multipleOf" if the result of the division of the instance by this keyword's value is an integer.

12

maxLength

The length of a string instance is defined as the maximum number of its characters.

13

minLength

The length of a string instance is defined as the minimum number of its characters.

14

pattern

A string instance is considered valid if the regular expression matches the instance successfully.

You can take a look at http://json-schema.org for the complete list of keywords that may be utilized in defining a JSON schema. The above schema may be used to test the validity of the subsequent JSON code −

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