Annotate and validate data structures with JSON Schema

Home » Blog » Software » Enterprise Software » Annotate and validate data structures with JSON Schema

I’d like to begin my little series of useful tools for developing CRUD applications with JSON Schema:

“JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.”

JSON Schema gives you a straightforward way to provide type information and validation to existing data structures. Say, for example you have a product instance like this (converted from your language’s native object or general data structure format using a tool like Jackson for Java):

[code lang=”js”] {
"id": 1,
"name": "A green door",
"price": 12.5,
"checked": false,
"tags": [
"home",
"green"
] }
[/code]

Then using a tool like this online JSON Schema editor you can have an object definition like this automatically generated for you:

[code lang=”js”] {
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {},
"id": "http://example.com/example.json",
"properties": {
"checked": {
"id": "/properties/checked",
"type": "boolean"
},
"id": {
"id": "/properties/id",
"type": "integer"
},
"name": {
"id": "/properties/name",
"type": "string"
},
"price": {
"id": "/properties/price",
"type": "number"
},
"tags": {
"id": "/properties/tags",
"items": {
"id": "/properties/tags/items",
"type": "string"
},
"type": "array"
}
},
"type": "object"
}
[/code]

Now instead of just a loosely-typed set of properties you have an interchangeable message format with strict types like number, string or array, which allows machines to automatically process and make inferences about your existing data structures regardless of programming language and view technology. By using another tool like JSON Editor, for instance, you can automatically generate HTML forms and CRUD UIs just from a mere schema definition! Tools like Angular Schema Form (for AngularJS 1.x) and JSONForms (both AngularJS 1.x and Angular >= 2.x) even allow you to have forms within a complex application generated automatically from JSON Schema definitions.

In addition to type definitions and nested structures JSON Schema also accounts for formats like time, date-time, email or phone (see this example) and validations like minimum (see here for a complete walk-through), which gives you the added benefit of CRUD UIs that automatically validate entered data without you as a developer having to laboriously write validation rules by hand.

On top of that there’s JSON Hyper-Schema, which accommodates linked resources not unlike those defined by applications complying with the HATEOAS architecture pattern. See this business card example and the specification for more information on this.

About the author: Bjoern
Independent IT consultant, entrepreneur
2 Comments
  1. Pingback: JHipster: Generate And Bootstrap CRUD Apps With Spring Boot And Angular | Björn Wilmsmann

  2. Pingback: More on JSON and REST API Specifications | Björn Wilmsmann

Leave a Comment