I have to validate a JSON posted to an API endpoint. Before, the business logic of the service executes, I wanted to check whether the JSON is valid, and only if it is valid, I want to execute the service.
In the above JSON, we have specified, event4 as event_id as part of the key "PI", which is invalid because, in the "events" array, we don't have an entry for "event4". My requirement is to validate this and throw an error, when the value of "event_id" in "PI" array, does not match with that of the ID's in "events" array.
Can someone please guide me on how we will be able to achieve it?
I tried to use JSON-Schema, for validating this, but with that we can decide on the keys, which should be mandatory, but no where we will be able to check for the value in references.
One approach I thought of is, after transforming the JSON to a Java object, I need to check for the invalid entries, but felt that it will be cumbersome process, if in case the array of objects in "PI" and/or "events" get changed/increased.
Yes. I agree with that. We can make use of Jackson library and transform the JSON to a Java object. And by doing so, we can have a check for the values mapped for a variables in the Java object.
But, is there any way, so that, while binding the values of JSON to Java object, we can force this validation step.