4

I am trying to execute a SELECT SQL query and after that I need to loop through the ResultSet and get the columns and check whether the data I got back for those columns is a Valid JSON String or not.

Here columnsList is an ArrayList which will contain all the names of the Columns of a particular table.

ResultSet rs = preparedStatement.executeQuery();

while (rs.next()) {
    for (String column : columnsList.split(",")) {

        //check whether rs.getString(column) is a valid JSON String?
        if(rs.getString(column).ISvalid_JSON_String()) {

            System.out.println("Valid JSON String data")
        }
    }
}

I am not sure how should I check whether the data I got back for each column is a valid JSON String or not?

Any thoughts?

  • 1
    See http://stackoverflow.com/questions/10174898/how-to-check-whether-the-given-string-is-valid-json-or-not-in-java – Mike Q Feb 27 '13 at 23:15

2 Answers2

3

If you need to be sure it really is valid JSON you're going to need to parse it. A fast, simple, lightweight parser that I like is json-simple. Have a look at their examples here.

http://code.google.com/p/json-simple/wiki/DecodingExamples#Example_2_-_Faster_way:_Reuse_instance_of_JSONParser

Adapting your code I get:

JSONParser parser = new JSONParser();
ResultSet rs = preparedStatement.executeQuery();

while (rs.next()) {
    for (String column : columnsList.split(",")) {
        //check whether rs.getString(column) is a valid JSON String?
        try{ 
            parser.parse(rs.getString(column)); 
            System.out.println("Valid JSON String data");
        } catch (ParseException e) {
            System.out.printlnn("Invalid JSON String data");
        }
    }
}
Gus
  • 6,719
  • 6
  • 37
  • 58
1

I prefer using the Jackson library its good at handling big files.

   import com.fasterxml.jackson.databind.ObjectMapper;
   import java.io.IOException;

    private Boolean isValidJson(String maybeJson){
        try {
            final ObjectMapper mapper = new ObjectMapper();
            mapper.readTree(maybeJson);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

I've written some test to check the behaviour of this approach.

    Assert.assertThat(isValidJson("{\"token_type\":\"bearer\",\"access_token\":\"AAAA%2FAAA%3DAAAAAAAA\",\"scope\": \"scope of the token\",\"expires_in\": 200,\"refresh_token\":\"fdb8fdbecf1d03ce5e6125c067733c0d51de209c\"}"), Is.is(true));
    Assert.assertThat(isValidJson("[ \"Ford\", \"BMW\", \"Fiat\" ]\n"), Is.is(true));
    Assert.assertThat(isValidJson(""), Is.is(false));
    Assert.assertThat(isValidJson("Lachlan"), Is.is(false));
    Assert.assertThat(isValidJson("{ key: value }"), Is.is(false));

Integrate this solution back into your code. I've assumed that you'll be putting the validation code in the same class.

while (rs.next()) {
    for (String column : columnsList.split(",")) {
        String maybeJson = rs.getString(column)
        //check whether rs.getString(column) is a valid JSON String?
        if(isValidJson(maybeJson)) {
            System.out.println("Valid JSON String data")
        }
    }
}

Consider adding a null check depending on what values and desired behaviour.

Lance
  • 768
  • 7
  • 21