How to do comparison predicates with string values?

38 views
Skip to first unread message

athomewith...@gmail.com

unread,
Apr 13, 2016, 4:40:44 PM4/13/16
to JsonPath
Let's say I have a JSON snippet that looks like this :

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

Now, let's say I have some code that looks like this :

   public boolean passesJsonPath(Map<String, Object> jsonObject, String jsonPath) {

      

      try {

         

         Configuration conf = Configuration.builder().options(Option.AS_PATH_LIST).build();

         List<String> list = JsonPath.using(conf).parse(jsonObject).read(jsonPath);

         return true;

         

      } catch (PathNotFoundException pathNotFoundException) {

         

         return false;

      }

   }


A call to passesJsonPath() will return true if I pass in this value for the jsonPath argument :

$.[?(@.age > 10)]


However, let's say that, in the JSON snippet, age is a quoted string, so that it looks like this : 

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : "26",
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

In this case, my call to passesJsonPath() will return false, since it will try to compare the string value of "26" to the number value of 10.  Let's assume that age will always be passed in as a quoted string, and I have no control over that.  Is there any way I can get JsonPath to treat the quoted string "26" as a number?  Is there any change I can make to my predicate that will make this possible?  Any configuration option I can use?

Thanks for taking a look at this!

--Jeremy

Reply all
Reply to author
Forward
0 new messages