• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

REST API Tutorial

  • REST
  • JSON
  • Dark Mode
Home / JSON / JSON Schema

JSON Schema

It’s often necessary for applications to validate JSON objects, to ensure that required properties are present and that additional constraints (such as a price never being less than one dollar) are met. These validations are typically performed in the context of JSON Schema.

Syntactic vs Semantic Validation

When we validate a JSON document without it’s Schema, we are validating only the syntax of the document. Syntactic validation guarantees only that the document is well-formed.

The tools such as JSONLint, and the JSON parsers perform only Syntactic validations.

Semantic validation is more than syntactic validation. It performs the syntax checks as well as the data checks.

Semantic validation helps in ensuring that the client is sending only allowed fields in JSON document, and not any invalid name-value pairs.

It can also help in checking the format of a phone number, a date/time, a postal code, an email address, or a credit card number.

What Is JSON Schema?

JSON Schema is a grammar language for defining the structure, content, and (to some extent) semantics of JSON objects. It lets you specify metadata (data about data) about what an object’s properties mean and what values are valid for those properties.

The result of applying the grammar language to a JSON document is the schema (a blueprint) describing the set of JSON objects that are valid according to the schema.

  1. JSON Schema is itself a JSON object.
  2. JSON Schema grammer is maintained at http://json-schema.org.
  3. It describes the existing data format.
  4. If offers clear, human-readable, and machine-readable documentation.
  5. It provides complete structural validation, which is useful for automated testing and validating client-submitted data.

JSON Schema Validation Example

Let’s consider the following JSN schema :

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "Person",
   "description": "A person",
   "type": "object",
   "properties":
   {
      "name":
      {
         "description": "A person's name",
         "type": "string"
      },
      "age":
      {
         "description": "A person's age",
         "type": "number",
         "minimum": 18,
         "maximum": 64
      }                                                                                
   },
   "required": ["name", "age"]    
}

and JSON document is :

{
   "name": "John Doe",
   "age": 35
}

The JSON Schema website provides links to various validator implementations for different programming languages (see http://json-schema.org/implementations.html ). We can download an implementation and integrate it into our application, subject to licensing requirements.

We have used the online validator at JSONSchemaLint in the given example.

Valid JSON document example

Valid JSON
Valid JSON

Invalid JSON document example (age > 64)

Invalid JSON
Invalid JSON

Was this article helpful?

Share this:

  • Twitter
  • Facebook
Previous Tutorial:
JSON with JSONPath
Next Tutorial:

Reader Interactions

Comments

  1. Mahesh says

    November 6, 2019 at 1:43 pm

    i want to POST the JSON data for REST API to update the User details based on ID and i need to do for multiple Id’s.

    [
    {“customFieldId”: 35, “value”: “EmployeeTeam2”},
    {“customFieldId”: 36, “value”: “EmployeeTeam2”}
    ]

    below one is working.

    {“customFieldId”: 35, “value”: “EmployeeTeam2”}

    but how would i pass the multiple values in single JSON variable ???

    Reply
    • Admin says

      November 6, 2019 at 5:20 pm

      Both inputs are valid. Just make sure your API expects this array as well.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search Tutorials

JSON Tutorial

  • What is JSON
  • JSON Syntax
  • JSON Data Types
  • JSON Schema
  • JSON Object
  • JSON Array
  • JSON parse()
  • JSON stringify()
  • JSON vs XML
  • JSON with Ajax
  • JSONPath

Footer

References

  • The dissertation by Roy Thomas Fielding
  • Uniform Resource Identifier (URI, URL, URN) [RFC 3986]
  • Internet MediaTypes
  • Web Application Description Language (WADL)

Meta Links

  • About
  • Contact Us
  • Privacy Policy

Blogs

  • How To Do In Java

Copyright © 2020 · restfulapi.net · All Rights Reserved. | Sitemap