title: Reliable JSON string validator in Java
body: <p>I need some help coming with a reliable JSON string validator - a method which intake a string and checks if it's a valid JSON. Example: if I pass <code>{"color":"red"}</code> or <code>{"amount":15}</code> it will pass but something like <code>"My invalid json"</code> will not. In short I need something that is as reliable as www.jsonlint.com validator. BTW - I'm not interested in deserializing into java object, because that's not my requirement. I may receive an arbitrary string and all I have to do is validate it has a valid JSON format.</p>
  
  <p>I have already researched on this forum several posts on the subject regarding java JSON string validations.</p>
  
  <p>What I have done so far:</p>
  
  <p>I tried using these classes: <code>org.json.JSONObject</code> and <code>org.json.JSONArray</code> in the following manner:</p>
  
  <pre><code>private static boolean isValidJSONStringObject(String requestBody){
      try {
          new JSONObject(requestBody);
      } catch (JSONException jsonEx) {
          return false;
      }
      return true;
  }
  
  private static boolean isValidJSONStringArray(String requestBody) {
      try {
          new JSONArray(requestBody);
      } catch (JSONException jsonEx) {
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>However, the following strings (entire lines) still go through, and they shouldn't:</p>
  
  <pre><code>{"color":"red"}{"var":"value"}
  
  [1,2,3][true,false]
  </code></pre>
  
  <p>in other words when I have objects/arrays repeated w/out any encapsulation in some parent object. If you paste these lines in www.jsonlint.com validator they both fail.</p>
  
  <p>I know there is always a regex option but I gess that cannot be guaranteed 100% because of the recursive nature of JSON and those regex expressions are going to be rather complex.</p>
  
  <p>Any help will be greatly appreciated!</p>