Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JSON mode


May 08, 2021 JSON


Table of contents


JSON Mode (Schema)

The JSON pattern is a specification that defines the JSON data structure based on the JSON format. I t was written under the draft IETF and expired in 2011. JSON mode:

  • Describes the existing data format.
  • Clean human and machine-readable documents.
  • Complete structural verification for automated testing.
  • Complete structural validation that can be used to validate data submitted by the client.

JSON pattern validation library

There are several validators available for different programming languages. But the most complete and compatible JSON-compatible validator today is the JSV.

Language The library
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); r uby-jsonschema (MIT)
Php php-json-schema (MIT). j son-schema (Berkeley)
Javascript Orderly (BSD); J SV; j son-schema; M atic (MIT); D ojo; P ersevere (modified BSD or AFL 2.0); s chema.js.

Example of JSON pattern

Here's a basic JSON pattern that covers a classic 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 take a look at the various important keywords that can be used in this pattern:

Keywords Describe
$schema $schema the keyword status to indicate that the pattern is consistent with the draft v4 specification.
title Use it to provide a title for our pattern.
description A description of the pattern.
type The type keyword defines the first constraint on our JSON data: it must be a JSON object.
properties Define the various keys and their value types, as well as the minimum and maximum values used in the JSON file.
required Store a list of necessary properties.
minimum The constraints that are set to the value, which represent the minimum acceptable value.
exclusiveMinimum If "exclusiveMinimum" exists and has a Boolean value true, the instance is valid if it is strictly greater than the value "minimum".
maximum The constraints that are set to the value, representing the maximum acceptable value.
exclusiveMaximum If "exclusiveMinimum" exists and has a Boolean value true, the instance is valid if it is strictly less than "maximum".
multipleOf If the result of splitting an instance by the value of this keyword is a number, the digital instance next to "multipleOf" is valid.
maxLength The maximum length value of the string instance character.
minLength The minimum length value of the string instance character.
pattern If the regular expression matches the instance successfully, the string instance is considered valid.

You can http://json-schema.org list of keywords that you can use to define the JSON pattern on the http://json-schema.org of the list. The above pattern can be used to test the validity of the JSON code given below:

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