DEV Community

Cover image for Querying and validating a JSON document in Java
JJBRT
JJBRT

Posted on • Edited on

Querying and validating a JSON document in Java

The JSON format is now the standard for communication between distributed services and there are various solutions for reading and validating these documents: here we will present a new one compatible with the latest Java versions: the Burningwave JSON library.

To start we need to add the following dependency to our pom.xml:

In this library there are 3 types of objects that respectively deal with reading, querying and validating JSON documents:

To explain how these types of objects work we will take the following JSON document as input:

The ObjectHandler

First of all, to find values in a JSON document we need to load it via ObjectHandler. The ObjectHandler wraps the JSON document and contains the path and the value of the node you are visiting within the JSON. To instantiate an ObjectHandler follow this code:

The Finder

After loaded the JSON document, to search elements within it we need to instantiate a Finder. There are 3 kinds of Finder:

The ObjectHandler.Finder

To obtain this kind of finder we need to use this code:

Once we obtained the finder we can use it to search items inside the JSON document:

The ObjectHandler.ValueFinder

To obtain this kind of finder we need to use this code:

Once we obtained the finder we can use it to search items inside the JSON document:

The ObjectHandler.ValueFinderAndConverter

To obtain this kind of finder we need to use this code:

Once you obtained the finder we can use it to search items inside the JSON document and convert them:

The Validator

To validate a JSON document we need to obtain the Validator and then register the checks:

Once registered the checks, to execute the validation you must call the validate method:

The search and validation possibilities offered by this library are practically infinite and this tutorial only shows an overview of the components exposed by the library and some examples of basic operation: for any further help you can open a discussion on GitHub.

From here you can download/clone the tutorial shared on GitHub.

Top comments (0)