Reputation: 461
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 {"color":"red"}
or {"amount":15}
it will pass but something like "My invalid json"
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.
I have already researched on this forum several posts on the subject regarding java JSON string validations.
What I have done so far:
I tried using these classes: org.json.JSONObject
and org.json.JSONArray
in the following manner:
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;
}
However, the following strings (entire lines) still go through, and they shouldn't:
{"color":"red"}{"var":"value"}
[1,2,3][true,false]
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.
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.
Any help will be greatly appreciated!
Upvotes: 10
Views: 14178
Reputation: 3
Paste your string here. See the output.
EDIT:
The link above does not work anymore, this is a good alternative.
Upvotes: -3
Reputation: 461
Here is our solution for now. Using the two different libraries (gson - first private method and jackson - second private method) is not ideal but at least we are passing all of our unit/integration tests. I bet we can do all we need with just the jackson tools.
public static boolean isStringValidJSON(String jsonString) {
return (isJSONStringObjectOrArray(jsonString) && isJSONStringParsable(jsonString));
}
private static boolean isJSONStringObjectOrArray(String jsonString) {
try {
JsonElement element = new JsonParser().parse(jsonString);
return (element.isJsonObject() || element.isJsonArray());
} catch (JsonSyntaxException jsonEx) {
return false;
}
}
private static boolean isJSONStringParsable(String jsonString) {
try {
org.codehaus.jackson.JsonParser parser =
new ObjectMapper().getJsonFactory().createJsonParser(jsonString);
while(parser.nextToken() != null) {
}
return true;
} catch (JsonParseException e) {
return false;
} catch (IOException e) {
return false;
}
}
Upvotes: 3
Reputation: 80603
Gson can handle this. Here is an example:
public boolean isValid(String json) {
try {
new JsonParser().parse(json);
return true;
} catch (JsonSyntaxException jse) {
return false;
}
}
String json = "{\"color\":\"red\"}{\"var\":\"value\"}";
System.out.println(isValid(json));
Note that Gson does allow some leniency in input JSON, which might not be desired. For example, unquoted keys are automatically converted to quoted ones by the parser. This may or may not be a deal-breaker depending on your expected usage.
Upvotes: 6