Querying and validating a JSON document in Java
Tags: json, querying, validating, java
Published: 2023-10-25 09:19:56

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**](https://burningwave.github.io/json/) library.

To start we need to add the following dependency to our _pom.xml_:

{% gist https://gist.github.com/Roberto-Gentili/679ca2c198c5410d809f630b6675234e.js %}

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

- the [**ObjectHandler**](#the-objecthandler)
- the [**Finder**](#the-finder)
- the [**Validator**](#the-validator)

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

{% gist https://gist.github.com/Roberto-Gentili/a6207f79fd427a932739bbe55c4e7312.js %}

## 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:
{% gist https://gist.github.com/Roberto-Gentili/d9fb333bdeb17685cff9607f139589eb.js %}

## 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**](#the-objecthandlerfinder) that which allows you to search for elements within the JSON ​​returning ObjectHandlers
* the [**ObjectHandler.ValueFinder**](#the-objecthandlervaluefinder) that which allows you to search for elements within the JSON ​​directly returning the values
* the [**ObjectHandler.ValueFinderAndConverter**](#the-objecthandlervaluefinderandconverter) that which allows you to search for elements within the JSON ​​and convert the values found

### The ObjectHandler.Finder
To obtain this kind of finder we need to use this code:
{% gist https://gist.github.com/Roberto-Gentili/02e5deb76ee53ee8ede5c559644d1013.js %}

Once we obtained the finder we can use it to search items inside the JSON document:
{% gist https://gist.github.com/Roberto-Gentili/8a5a6793cb1282360db7b74b4a4e3568.js %}

### The ObjectHandler.ValueFinder
To obtain this kind of finder we need to use this code:
{% gist https://gist.github.com/Roberto-Gentili/e0f3d235aa83e45c49a977e50bb65b74.js %}

Once we obtained the finder we can use it to search items inside the JSON document:
{% gist https://gist.github.com/Roberto-Gentili/424ca9dfa55e8edb7cb34232122b0795.js %}

### The ObjectHandler.ValueFinderAndConverter
To obtain this kind of finder we need to use this code:
{% gist https://gist.github.com/Roberto-Gentili/db4b4024ad2a69c1fa5bde58bd087de7.js %}

Once you obtained the finder we can use it to search items inside the JSON document and convert them:
{% gist https://gist.github.com/Roberto-Gentili/b98ff0b1ef5882b8f6bd98f4d6885dbc.js %}

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

{% gist https://gist.github.com/Roberto-Gentili/717cfbacc79bb98a18be450ac47b4420.js %}

Once registered the checks, to execute the validation you must call the `validate` method:
{% gist https://gist.github.com/Roberto-Gentili/9590b8d7c04b3f065b2a4084d4b6b5ee.js %}

**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](https://github.com/burningwave/json/discussions).

[**From here**](https://github.com/JJBRT/advanced-java-tutorials) you can download/clone the [**tutorial shared on GitHub**](https://github.com/JJBRT/advanced-java-tutorials/tree/master/json).