JSON schema validator library in Java

17,032

Projects worth exploring:

Here is a nice list.

Here is an online sandbox.

Share:
17,032

Related videos on Youtube

user1457881
Author by

user1457881

Updated on June 14, 2022

Comments

  • user1457881
    user1457881 almost 2 years

    I have a restful web service(JAVA) which has to accept JSON requests. I have to first validate this JSON against a JSON schema that I have. I'm not sure what is the best JAVA library to validate JSON again JSON schemas. I have used json-schema-validator-2.1.7 library but it has not been very helpful. Even thought my JSON is not a valid JSON I do not get any errors.

    Here is the code I use for json-schema-validator-2.1.7

    InputStream jsonSchemaInputStream = Assessment.class.getClassLoader().getResourceAsStream("Schemas/AssessmentMetrics.json");
    ObjectMapper mapper = new ObjectMapper();
    
    // Allows to retrieve a JSONSchema object on various sources
    // supported by the ObjectMapper provided
    JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);
    
    // Retrieves a JSON Schema object based on a file
    JSONSchema schema = schemaProvider.getSchema(jsonSchemaInputStream);
    
    // Validates a JSON Instance object stored in a file
    List<String> errors = schema.validate(contents);
    
    • Admin
      Admin over 10 years
      as stated by Francis here groups.google.com/forum/#!topic/json-schema/ew_s7G9PoxE (guy who wrote the library you're using), it seems that the most complete option in java for JSON Schema validation is his library... well, you may want to try jackson validation module too. Also, remember that 2.1.7 is not the stable version, but 2.0.1 (according to github.com/fge/json-schema-validator). That said, I'd ask you to add to your question a sample of your schema and the json you're trying to validate.
  • Luis Casillas
    Luis Casillas almost 8 years
    Reading that link, it doesn't appear to be a JSON Schema validator. It assumes you have annotated Java classes, and it will examine those and generate a JSON schema that all of the serialized JSON will obey. Whereas the poster's question is the opposite scenario—somebody's given them a JSON Schema and a JSON document, and they want to check whether the document satisfies the schema.

Related