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):

{
    "id": 1,
    "name": "A green door",
    "price": 12.5,
    "checked": false,
    "tags": [
        "home",
        "green"
    ]
}

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

{
    "$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"
}

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.

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

* Checkbox GDPR is required

*

I agree

By continuing to browse the site you agree to our use of cookies. Privacy Policy

Privacy Preference Center

Strictly necessary

These cookies are necessary for the site to function.

PHPSESSID: Preserves user session state across page requests.

__cfduid: Used by the content network, Cloudflare, to identify trusted web traffic.

PHPSESSID
__cfduid

Preferences

Remembers the user's submitted data when a comment is submitted in a blog post. The purpose is to aut o-populate form fields for subsequent comments, in order to save time for the user.

wfvt_#

Statistics

Statistic cookies help us to understand how visitors interact with our websites by collecting and reporting information anonymously.

_ga: Registers a unique ID that is used to generate statistical data on how the visitor uses the website.

_gat: Used by Google Analytics to throttle request rate.

_gid: Registers a unique ID that is used to generate statistical data on how the visitor uses the website.

collect: Used to send data to Google Analytics about the visitor's device and behaviour. Tracks the visitor across d evices and marketing channels.

_ga,_gat,_gid
collect

Security

We use Wordfence to secure our website against hacking attempts: https://www.wordfence.com/

wordfence_verifiedHuman

Close your account?

Your account will be closed and all data will be permanently deleted and cannot be recovered. Are you sure?