5

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

2 Answers 2

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");
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I am not looking to parse the Valid JSON String. Just check to see whether it's a valid or not.
What is your definition of valid? (most people consider valid to mean it can be parsed)
@FarhanJamal: You have to parse it to know if it's valid
This validates { key: value } too, but its not a valid json
@HemantPatel I've provided a solution that correctly identified this as invalid json.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.