Hi,
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.
Requirement:
JSON structure:
{
"id": "1",
"name": "ABC",
"events": [
{
"id": "event1",
"name": "Event 1"
"props": .......
},
{
"id": "event2",
"name": "Event 2"
"props": .......
},
{
"id": "event3",
"name": "Event 3"
"props": ........
}
],
"PI": [
{
"pi_id": "123",
"pi_name": "ABC",
"event_id": "event1"
},
{
"pi_id": "456",
"pi_name": "DEF",
"event_id": "event2"
},
{
"pi_id": "789",
"pi_name": "XYZ",
"event_id": "event4"
}
]
}
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.
Thanks,
Ashwin