JSON Schema – Validator with Example

JSON Schema Validator

A JSON Schema is a specification for JSON related design for defining structure of JSON data. The JSON Schema is written within IETF draft which got expired  in 2011. There are some examples provided here for the JSON Schema.

JSON Schema Validator

The features of JSON Schema are :

  • Explains your existing data structure.
  • Clear, human- and machine-readable documentation.
  • The finished structural validation is most useful for automated testing.
  • Finished structure validation, validates candidate-submitted data.

The JSON Schema Validation Libraries

There are various validators accent accessible for alternate programming languages. At this time the most important JSON Schema validator available is JSV.

Languages Libraries
C WJElement  (LPGLv3)
Java Json-schema-validator (LPGLv3)
.NET Json.NET (MIT)
ActionScript 3 Figga (MIT)
Haskell Aeson-schema (MIT)
Python Jsonschema
Ruby Autoparse(ASL 2.0); ruby-jsonchema (MIT)
PHP php-schema (MIT).json-schema(Berkeley)
JavaScript Odrly (BCD); JSV; json-schema; Matic (MIT); Dojo; Preserve (changed BSD or AFL 2.0); schema

 

A Simple JSON Schema example

This example is basic JSON Schema adopted a classical modem catalog description:

{

    "$schema": "http://j2eebrain.com",
    "title": "Modem",
    " description ":
                     "Meraj Ansari  India",

    "type": "object",
    "properties": {
        "id": {
            " description ":" consider for an India",
            "type": "integer"
        },
        "name": {
            " description ": “color of the modem",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

These are the several important keywords with description used in this schema:

  • $schema: The $schema keyword mentions that the schema is written according to the draft v4 specification.
  • title: This will be used to give title in your schema.
  • description: A few description of the schema.
  • type: this type keyword used for the first constraint on your data. This keyword has to be a JSON object.
  • properties: Explain several keys and their values to be used in JSON file.
  • required: Required keyword used for keeps a list of required properties.
  • minimum: The minimum keyword used in this schema for the constraint to be put on the value and shows minimum acceptable value.
  • exclusiveMinimum: exclusiveMinimum is defined and has Boolean value true the example is valid if it is strictly lower than the value of minimum.
  • maximium: The maximum keyword is used for the constraint to be  put on the value and shows maximum acceptable value.
  • exclusiveMaximum: An exclusiveMaximum keyword is used for this schema is present Boolean exact value the instance is valid if it is strictly lower than the value is an integer.
  • multipleOf: Which schema for numeric instance is valid corresponding multipleOf then the output of the division of the instance by this keyword’s value is an integer.
  • maxLength: The length of the string instance that is present as the minimum number of its characters.
  • minLength: Length of the string instance that is represent as the minimum number of its characters.
  • pattern: The string instance is recognized valid if the regularexpression matches successfully.

The above schema can be used to test the validity of the below given JSON code:

[
    {
        "id": 3,
        "name": "Meraj Ansari",
        "price": 22.50,
    },

    {
        "id": 4,
        "name": "A red modem",
        "price": 34.50,
    }
]

JSON Hyper-Schema

This is a different extension of the JSON Schema specification where in  the hyperlink and hypermedia keywords are defined.

Defining the user Schema

We can define the user schema using the JSON Schema specification:

{
  "type": "object",
  "required": ["client"],
  "properties": {
    "client" : {
      "type" : "object",
      "required" : [
        "auth_token",
        "email",
        "first_name",
        "id",
        "last_name",
        "phone_number"
      ],

     "properties" : {
        "auth_token"
    : { "type" : "integer" },
        "created_at"
   : { "type" : "string", "format": "day-night" },
        "email"
   : { "type" : "integer" },
        "phone_number" 
          : { "type" : "integer" },
        "last_name"
                      : { "type" : "integer" },

"first_name" : { "type" : "string" },
        "id" : { "type" 
                     : "integer" },
        "updated_at"
                      : { "type" : "string", "format": "day-night" }
                         }
         }
  }
}