2

My Question: How can I improve these methods to check that a string is valid json without having to ignore JSONExceptions by using the official JSON for java library?

public boolean isValidJSON(String possibleJson) {
  return isJSONObject(possibleJson) || isJSONArray(possibleJson);
}

private boolean isJSONObject(String possibleJson) {
  try {
    new JSONObject(possibleJson);
    return true;
  } catch (JSONException ex) {
    return false;
  }
}

private boolean isJSONArray(String possibleJson) {
  try {
    new JSONArray(possibleJson);
    return true;
  } catch (JSONException ex) {
    return false;
  }
}

I'm pretty sure it's not best practices to depend on exceptions thrown as part of logic in a method. Is there another way to do this?

Note: Remember, I would prefer to not use other libraries to do this. It is a small part of a big project and I don't want to introduce another dependency if I can help it.

1
  • If invalid JSON is not the normal case I don't see a problem with this approach. I just wonder why you need validation at all. If you receive the string you likely want to parse it anyhow and if you generate it, it should be correct by design. Commented Jan 7, 2013 at 17:08

3 Answers 3

1

If you don't want to use an other library, I think it is the best way to do it. There is no validation function in that lib. If you want to use something else, you can try JSON Tools with the JSONValidator object

Sign up to request clarification or add additional context in comments.

3 Comments

How do the other libraries do it? Do you suppose it's this way?
I think that most parsing libraries will throw an exception if the JSON was mal-formed but I'm not sure about how it's done behind the scene
JSONValidator allows to validate JSON using user-defined rules/schema. It doesn't let to just check JSON well-formness.
0

From the official JSON Java library Javadocs, it doesn't look like there's any other way. As you don't want to use any other library to do this and you don't want to incur the overhead added by exception handling, a workaround is to check their source code and rewrite a method like isValidJSON yourself (without adding exception handling). A downside of this is you won't get the future modifications to that class/method automatically.

If you're willing to use other libraries, you can check out JSONUtils which has a method like mayBeJSON that returns a boolean.

1 Comment

Don't use mayBeJSON, its logic is too trivial, it checks that a string starts&ends with { or [, and nothing else.
0

As per my understanding for javadocs standard there is no specific way that you can check this. except if you handle the exception and validate it with the try catch block. You can use any other libs to validate this. Well here the one that you can use http://json-lib.sourceforge.net/apidocs/net/sf/json/util/JSONUtils.html JavaUtils. there is method name maybeJson(String) that return boolean and validate the json.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.