• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Validation of JSON posted to REST endpoint

 
Ashwin Ganesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joseph Mokenela
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think your best bet will be to validate on the Java side. You can have a look at Jackson. It simplifies the marshaling of Java Objects to and from JSON.
 
Ashwin Ganesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic