mscharhag, Programming and Stuff;

A blog about programming and software development topics, mostly focused on Java technologies including Java EE, Spring and Grails.

Tuesday, 21 July, 2020

Validating and documenting JSON with JSON Schema

JSON Schema is a way to describe a JSON document. You can think of XML Schema for JSON. It allows you to define required elements, provide validation constraints and documentation.

I think the easiest way to explain JSON Schema is to look at an example snippet of JSON and the corresponding JSON Schema. So, I created an image that shows both and is hopefully self explanatory:

json-schema

(Click to enlarge, if you are on a mobile device)

A JSON Schema validator can be used to validate a JSON document against a JSON Schema. This can be done online (e.g. https://www.jsonschemavalidator.net/) or using a library of your favourite programming language. In the implementations section of json-schema.org you can find a couple of libraries to work with JSON Schema.

In case you want to copy/paste some of the sections of the image: You can find the example as text on GitHub.

Leave a reply