17

I am having trouble finding the most simple way to validate a JSON String against a given JSON-schema String (for reference, this is in Java, running in an Android app).

Ideally, I'd like to just pass in a JSON String and a JSON-schema String, and it returns a boolean as to whether it passes the validation. Through searching, I have found the following 2 promising libraries for accomplishing this:

http://jsontools.berlios.de/

https://github.com/fge/json-schema-validator

However, the first one seems fairly outdated with poor support. I implemented the library into my project, and even with their JavaDocs, I was unable to tell how to properly build a "Validator" object for validation.

Similar story with the 2nd one, which seems to be up-to-date with good test code. However, for what I want to do, which is very simple, it seems to be a bit daunting and confusing as to how to specifically accomplish what I want (even after looking at theValidateServlet.java file).

Curious if anyone has any other suggestions on a good way to accomplish this (from what it seems), simple task that need, or if I perhaps need to stick with the 2nd option from above? Thanks in advance!

askedJan 24, 2013 at 21:51
svguerin3's user avatar
5
  • 2
    Author of json-schema-validator here... Didn't you see in the README that there was a link to code samples? ;)CommentedJan 28, 2013 at 5:32
  • Hi there, and thank you for your great library! Yes indeed, I did see the code samples and actually mentioned it in my post with an embedded link (the ValidateServlet.java file). Thanks again for this library! Great stuff :)CommentedFeb 11, 2013 at 21:31
  • I wasn't talking about this sample: I was talking aboutcom.github.fge.jsonschema.examples in the javadoc ;) BTW, 1.6.0 is out.CommentedFeb 12, 2013 at 0:05
  • 1
    Ah I did not notice that then! Thanks for the heads up on that, as well as the new version. :)CommentedFeb 12, 2013 at 6:01
  • @fge I cannot seem to get this working on Android targeting api 23CommentedOct 13, 2016 at 1:58

3 Answers3

14

A grateful thank you to Douglas Crockford and Francis Galiegue for writing the java-based json schema processor! And the on-line tester athttp://json-schema-validator.herokuapp.com/index.jsp is awesome! I really like the helpful error messages (I only found one example in which they failed), though line and column and/or context would be even better (right now, you only get line and column information during JSON format errors (courtesy of Jackson). Finally, I'd like to thank Michael Droettboom for hisgreat tutorial (even if he only covered Python, Ruby, and C while conspicuously ignoring the best language of all :-)).

For those who missed it (like I did at first), there are examples are at github.com/fge/json-schema-processor-examples. While these examples are very impressive, they are not the simple json validation examples that were originally requested (and that I too was looking for). The simple examples are at github.com/fge/json-schema-validator/blob/master/src/main/java/com/github/fge/jsonschema/examples/Example1.java

Alex's code above did not work for me, but was very helpful; my pom is pulling the latest stable release, version 2.0.1 with the following dependency inserted in my maven pom.xml file:

<dependency>    <groupId>com.github.fge</groupId>    <artifactId>json-schema-validator</artifactId>    <version>2.0.1</version></dependency>

Then the following java code works fine for me:

import java.io.IOException;import java.util.Iterator;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.databind.JsonNode;import com.github.fge.jsonschema.exceptions.ProcessingException;import com.github.fge.jsonschema.main.JsonSchema;import com.github.fge.jsonschema.main.JsonSchemaFactory;import com.github.fge.jsonschema.report.ProcessingMessage;import com.github.fge.jsonschema.report.ProcessingReport;import com.github.fge.jsonschema.util.JsonLoader;public class JsonValidationExample  {public boolean validate(String jsonData, String jsonSchema) {    ProcessingReport report = null;    boolean result = false;    try {        System.out.println("Applying schema: @<@<"+jsonSchema+">@>@ to data: #<#<"+jsonData+">#>#");        JsonNode schemaNode = JsonLoader.fromString(jsonSchema);        JsonNode data = JsonLoader.fromString(jsonData);                 JsonSchemaFactory factory = JsonSchemaFactory.byDefault();         JsonSchema schema = factory.getJsonSchema(schemaNode);        report = schema.validate(data);    } catch (JsonParseException jpex) {        System.out.println("Error. Something went wrong trying to parse json data: #<#<"+jsonData+                ">#># or json schema: @<@<"+jsonSchema+">@>@. Are the double quotes included? "+jpex.getMessage());        //jpex.printStackTrace();    } catch (ProcessingException pex) {          System.out.println("Error. Something went wrong trying to process json data: #<#<"+jsonData+                ">#># with json schema: @<@<"+jsonSchema+">@>@ "+pex.getMessage());        //pex.printStackTrace();    } catch (IOException e) {        System.out.println("Error. Something went wrong trying to read json data: #<#<"+jsonData+                ">#># or json schema: @<@<"+jsonSchema+">@>@");        //e.printStackTrace();    }    if (report != null) {        Iterator<ProcessingMessage> iter = report.iterator();        while (iter.hasNext()) {            ProcessingMessage pm = iter.next();            System.out.println("Processing Message: "+pm.getMessage());        }        result = report.isSuccess();    }    System.out.println(" Result=" +result);    return result;}public static void main(String[] args){    System.out.println( "Starting Json Validation." );    JsonValidationExample app = new JsonValidationExample();    String jsonData = "\"Redemption\"";    String jsonSchema = "{ \"type\": \"string\", \"minLength\": 2, \"maxLength\": 11}";    app.validate(jsonData, jsonSchema);    jsonData = "Agony";  // Quotes not included    app.validate(jsonData, jsonSchema);    jsonData = "42";    app.validate(jsonData, jsonSchema);    jsonData = "\"A\"";    app.validate(jsonData, jsonSchema);    jsonData = "\"The pity of Bilbo may rule the fate of many.\"";    app.validate(jsonData, jsonSchema);}}

My result from the above code is:

Starting Json Validation.Applying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"Redemption">#># Result=trueApplying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<Agony>#>#Error. Something went wrong trying to parse json data: #<#<Agony>#># or json schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@. Are the double quotes included? Result=falseApplying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<42>#>#Processing Message: instance type does not match any allowed primitive type Result=falseApplying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"A">#>#Processing Message: string is too short Result=falseApplying schema: @<@<{ "type": "string", "minLength": 2, "maxLength": 11}>@>@ to data: #<#<"The pity of Bilbo may rule the fate of many.">#>#Processing Message: string is too long Result=false

Enjoy!

answeredMar 7, 2014 at 18:05
Tihamer's user avatar
Sign up to request clarification or add additional context in comments.

Comments

11

This is essentially what the Servlet you linked to does, so it may not be a one-liner but it still is expressive.

useV4 anduseId as specified on the servlet, are for specifying validations option forDefault to draft v4 andUse id for addressing.

You can see it online:http://json-schema-validator.herokuapp.com/

public boolean validate(String jsonData, String jsonSchema, boolean useV4, boolean useId) throws Exception {   // create the Json nodes for schema and data   JsonNode schemaNode = JsonLoader.fromString(jsonSchema); // throws JsonProcessingException if error   JsonNode data = JsonLoader.fromString(jsonData);         // same here   JsonSchemaFactory factory = JsonSchemaFactories.withOptions(useV4, useId);   // load the schema and validate   JsonSchema schema = factory.fromSchema(schemaNode);   ValidationReport report = schema.validate(data);   return report.isSuccess();}
answeredJan 24, 2013 at 22:18
Alex's user avatar

2 Comments

Brilliant, thank you! I guess I probably just needed a nudge in the right direction on the proper way of using this library. Really appreciate it!
Note that the implementation detects$schema and defaults to draft v3. In most cases, you just useJsonSchemaFactory.defaultFactory().
3

@Alex's answer worked for me on Android but required me to Multi-dex and add:

    packagingOptions {        pickFirst 'META-INF/ASL-2.0.txt'        pickFirst 'draftv4/schema'        pickFirst 'draftv3/schema'        pickFirst 'META-INF/LICENSE'        pickFirst 'META-INF/LGPL-3.0.txt'    }

to mybuild.gradle

answeredOct 13, 2016 at 2:18
HaydenKai's user avatar

1 Comment

Thanks! Note:pickFirst is crucial here. Do not useexclude: although it makes compilation pass, it causes other issues because the code relies on the presence of the schema files.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.