DEV Community

Cover image for Json Schema and Json Validation
anilkulkarni87
anilkulkarni87

Posted on • Originally published at anilkulkarni.com

Json Schema and Json Validation

A sample json schema
A discussion in one of my office meetings, led me to think and write about Json Schema. Every post I write is an attempt to get a better understanding of the topic and keep that as a reference for my future projects.

How do you validate the complex nested Json data files?

One approach, I can think of is defining a schema and validating the JSON data against it. I had used this approach to validate a avro schema.

Define JSON Schema:

This is the most time consuming part but if done right, the JSON validation is cakewalk. Json Schema came to my rescue. After reading, I was able to understand most of the concepts and write my own schema and also validate a couple of data files. Below are some of the things you can define/achieve:

  • Mandatory values.
  • Nested objects and fields.
  • Data type and additional criteria:
    • String can be of format:email/uuid etc.
    • String can match with pattern.
    • Integer – Can have max , multipleOf.

JSON Validation

I leveraged a gradle plugin to implement the validation as part of the build. So that our next steps of the build are executed only if the Validation task is successful. The errors printed are very precise and easy to understand. You can find this sample project on my github.

Output from the gradle plugin.Validation Output

Next steps and other thoughts:

  • To add examples of different schemas Eg: Use definition and references.
  • One can also generate schema from a json and then improvise it.
  • For the first time, I used github actions to build the project.
  • I should write more about some of my other learnings.

The post Json Schema and Json Validation appeared first on Anil Kulkarni | Blog.

Top comments (2)

Collapse
 
nalani profile image
nalani5210 • Edited

Well written. I usually use JSON Formatter to handle JSON validation

Some comments may only be visible to logged-in visitors. Sign in to view all comments.