0

I have the following programming requirement:

problem: Given two JSONs A and B, if the fields x,y,z in JSON A match the fields i,o,p in B return true else false.

approach: I want to stay away from building a matching engine that depends on the json's format. I don't want to format the jsons by using pojos and then do object matching. My approach is to convert all the jsons into a hash map and then specify the location of the fields by using a string:

Example: money -> a,b,c

{
  a :
   {
      b : {
         c: {
           money : "100"
         }
       }
    }
}

However this approach seems to be a bit tricky as we have to take into account collections. I have to cover all of the edge cases. Is there any spring library or java tool I can use to fulfill this purpose?.

Fernando
  • 381
  • 1
  • 5
  • 20

2 Answers2

0

There are many libraries being used for this purpose.
The most popular one is com.google.gson
Usage:

JsonObject jo = (JsonObject)(jsonParser.parse("{somejsonstring}");<br>
jo.has("objectProperty") //Check if property exists
jo.get("objectProperty") // returns JsonElement, 
jo.get("objectProperty").isJsonArray() // check if the property is the type that want
jo.getAsJsonArray("objectProperty") get the property
İsmail Altun
  • 21
  • 1
  • 5
0

You may simplify this work by using im.wilk.vor:Voritem library gitHub or in Maven repository.

JsonElement je_one = jsonParser.parse("{some_json_string"})
JsonElement je_two = jsonParser.parse("{other_json_string"})
VorItem vi_one = vorItemFactory.from(je_one);
VorItem vi_two = vorItemFactory.from(je_two);
if (vi_one.get("a.b.c").optionalLong().isPresent() ) {
 return vi_one.get("a.b.c").optionalLong().equals(vi_one.get("i.o.p").optionalLong())
}

return false;