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.

JSON Syntax

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

1. Syntactic vs Semantic Validation

When we validate a JSON document without its 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 documents 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.

2. 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.

3. 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 implementation and integrate it into our application, subject to licensing requirements.

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

3.1. Valid JSON document example

Valid JSON
Valid JSON

3.2. Invalid JSON document example (age > 64)

Invalid JSON
Invalid JSON

Comments

Subscribe
Notify of
guest
2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Mahesh

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 ???