NinjaCat NinjaCat - 2 years ago 290
Java Question

How to Validate JSON with Jackson JSON

I am trying to use Jackson JSON take a string and determine if it is valid JSON. Can anyone suggest a code sample to use (Java)?

Answer Source

Not sure what your use case for this is, but this should do it:

public boolean isValidJSON(final String json) {
   boolean valid = false;
   try {
      final JsonParser parser = new ObjectMapper().getJsonFactory()
      while (parser.nextToken() != null) {
      valid = true;
   } catch (JsonParseException jpe) {
   } catch (IOException ioe) {

   return valid;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download