json-schema-validator
A JSON Schema validation implementation in pure Java, which aims for correctness and performance, in that order
Top Related Projects
A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Quick Overview
The java-json-tools/json-schema-validator is a Java library for validating JSON documents against JSON Schema specifications. It supports JSON Schema drafts 4, 6, 7, and 2019-09, providing a robust solution for JSON validation in Java applications.
Pros
- Supports multiple JSON Schema drafts, ensuring compatibility with various schema versions
- Offers extensive validation capabilities, including complex schema structures and custom keywords
- Actively maintained with regular updates and bug fixes
- Well-documented with clear usage instructions and examples
Cons
- Performance may be slower compared to some other JSON Schema validators
- Limited support for the latest JSON Schema draft (2020-12 at the time of writing)
- Dependency on other java-json-tools libraries, which may increase the project's footprint
- Learning curve for advanced features and custom keyword implementations
Code Examples
- Basic JSON Schema validation:
JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaJson);
ProcessingReport report = schema.validate(jsonNode);
if (report.isSuccess()) {
System.out.println("Validation successful");
} else {
System.out.println("Validation failed: " + report);
}
- Creating a schema from a file:
JsonNode schemaNode = JsonLoader.fromFile(new File("path/to/schema.json"));
JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaNode);
- Custom keyword implementation:
public class CustomKeywordValidator extends KeywordValidator {
@Override
public void validate(Processor processor, ProcessingReport report, MessageBundle bundle, JsonNode instance)
throws ProcessingException {
// Custom validation logic here
}
}
// Register the custom keyword
keyword = Keyword.newBuilder("customKeyword").withValidatorClass(CustomKeywordValidator.class).freeze();
JsonSchemaFactory factory = JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7))
.addKeyword(keyword).freeze();
Getting Started
To use the java-json-tools/json-schema-validator in your project, add the following dependency to your Maven pom.xml:
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.14</version>
</dependency>
For Gradle, add this to your build.gradle:
implementation 'com.github.java-json-tools:json-schema-validator:2.2.14'
Then, you can start using the library in your Java code as shown in the code examples above.
Competitor Comparisons
A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Pros of json-schema-validator (networknt)
- Better performance and lower memory usage
- Support for JSON Schema Draft 2019-09 and 2020-12
- More frequent updates and active maintenance
Cons of json-schema-validator (networknt)
- Less comprehensive documentation compared to json-schema-validator (java-json-tools)
- Fewer additional features beyond core validation functionality
Code Comparison
json-schema-validator (networknt):
JsonSchema schema = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V7).getSchema(schemaNode);
Set<ValidationMessage> validationResult = schema.validate(jsonNode);
json-schema-validator (java-json-tools):
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema(schemaNode);
ProcessingReport report = schema.validate(jsonNode);
Both libraries offer similar basic usage patterns for schema validation. The networknt version provides more flexibility in specifying the JSON Schema version, while the java-json-tools version uses a default factory method.
The networknt library returns a Set of ValidationMessage objects, which may be more convenient for processing individual validation errors. The java-json-tools library returns a ProcessingReport, which provides a more structured representation of validation results.
Overall, the networknt json-schema-validator is a good choice for projects prioritizing performance and up-to-date JSON Schema support, while the java-json-tools version may be preferred for its more extensive documentation and additional features.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Read me first
The current version of this project is licensed under both LGPLv3 (or later) and ASL 2.0. The old version (2.0.x) was licensed under LGPL 3.0 (or later) only.
Version 2.2 is out. See here for the list of changes compared to 2.0. And of course, it still has all the features of older versions.
What this is
This is an implementation with complete validation support for the latest JSON Schema draft (v4, including hyperschema syntax support) and the previous draft (v3 -- no hyperschema support though). Its list of features would be too long to enumerate here; please refer to the links above!
Should you wonder about it, this library is reported to work on Android. Starting with version 2.2.x, all APK conflicts have been resolved, so you can use this in this context as well.
Google Group
This project has a dedicated Google group. For any questions you have about this software package, feel free to post! The author (me) will try and respond in a timely manner.
Testing online
You can test this library online; this web site is in a project of its own, which you can fork and run by yourself.
Versions
- current stable version: 2.2.14 (ChangeLog, Javadoc, code samples).
- old stable version: 2.0.4 (ChangeLog, Javadoc, code samples).
Available downloads
Gradle/maven
This package is available on Maven central; the artifact is as follows:
Gradle:
dependencies {
compile(group: "com.github.java-json-tools", name: "json-schema-validator", version: "2.2.14");
}
Maven:
<dependency>
<groupId>com.github.java-json-tools</groupId>
<artifactId>json-schema-validator</artifactId>
<version>2.2.14</version>
</dependency>
"Full" jar; command line
OUTDATED: Let me know if you need this in the issues section.
This jar contains the library plus all its dependencies. Download the lib jar (a little more than 6 MiB) from Bintray.
Versioning scheme policy
The versioning scheme is defined by the middle digit of the version number:
- if this number is even, then this is the stable version; no new features will be added to such versions, and the user API will not change (save for some additions if requested).
- if this number is odd, then this is the development version; new features will be added to those versions only, and the user API may change.
Relevant documents
This implementation is based on the following drafts:
- JSON Schema Internet draft, version 4 (link to validation spec);
- JSON Schema Internet draft, version 3;
- JSON Reference Internet draft, version 3;
- JSON Pointer (RFC 6901).
More...
For a detailed discussion of the implementation, see here.
Please see the wiki for more details.
Top Related Projects
A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot