As far as I know Jackson can only produce schemas for given types, but not do validation. There is json-schema-validator but it is no longer maintained.

Answer from Martina on Stack Overflow
Top answer
1 of 4
6

As far as I know Jackson can only produce schemas for given types, but not do validation. There is json-schema-validator but it is no longer maintained.

2 of 4
3

1.) Add Dependency pom.xml :-

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

2.) NoSqlEntity is a meta data for entity that can reside in no-sql database.

Initialized NoSqlEntity with schema file.

public static final NoSqlEntity entity = new NoSqlEntity("PAYOUT_ENTITY", "DB_","/schema/payout_entity.json");

public class NoSqlEntity {
private static final Map<String, NoSqlEntity> STORE = new HashMap<>();

private final AtomicLong seq = new AtomicLong(System.currentTimeMillis());
private IdentityGenerator identityGenerator;
private String entity;
private String collectionName;
private String jsonSchema;
private String idColumn = "id";
private String database;


public NoSqlEntity(String entity, String idColumn, String collectionPrefix,      String jsonSchema) {
    this.entity = entity;
    this.idColumn = idColumn;

    this.collectionName = collectionPrefix + "_" + entity;
    this.jsonSchema = jsonSchema;
    STORE.put(entity, this);
}

public NoSqlEntity(String collectionName, String jsonSchema) {
    this.collectionName = collectionName;
    this.jsonSchema = jsonSchema;
}

public static NoSqlEntity valueOf(String entityType) {
    return STORE.get(entityType);
}

public boolean isNotNullSchema() {
    return jsonSchema != null;
}
 ...
 // Other Getter/Setter properties and methods.
}

3.) Sample format of validation schema file of payout_entity.json-

   {
    "properties":{
          "txId":{"type":"string"}

    }
    "required" :["txId","currency"]
  }

4.) JsonSchemaManager - Validate the incoming JSON schema and cache the schema as well.

public class JsonSchemaManager {
private final static Logger LOGGER = LoggerFactory.getLogger(JsonSchemaManager.class);
protected final static String LS = StandardSystemProperty.LINE_SEPARATOR.value();

private final JsonValidator validator = JsonSchemaFactory.byDefault().getValidator();
private final Map<NoSqlEntity, JsonNode> schemaMap = new HashMap<>();

public JsonNode load(NoSqlEntity noSqlEntity) throws IOException {
    final JsonNode schema = JsonLoader.fromURL(this.getClass().getResource(noSqlEntity.getJsonSchema()));
    schemaMap.put(noSqlEntity, schema);
    return schema;
}

public void validateSchema(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<ProcessingReport> consumer) {
    try {
        JsonNode schema = schemaMap.get(noSqlEntity);
        if (schema == null) {
            schema = load(noSqlEntity);
        }
        final ProcessingReport report = validator.validate(schema, toBeValidated);
        if (!report.isSuccess()) {
            consumer.accept(report);
        }
    } catch (IOException ex) { //NOSONAR
        throw new InvalidRequestException(ex.toString());
    } catch (ProcessingException ex) { //NOSONAR
        throw new InvalidRequestException(ex.toString());
    }
}

 public synchronized boolean synchronizedCheck(NoSqlEntity noSqlEntity, JsonNode toBeValidated, Consumer<Map<String, Object>> messageConsumers) {
    boolean flags = CommonUtils.unchecked(() -> {
        validateSchema(noSqlEntity, toBeValidated, report -> {
            report.forEach(processingMessage -> messageConsumers.accept(JsonConverter.jsonAsMapObject(processingMessage.asJson())));
        });
        return true;
    }, ex -> {
        throw new RuntimeException(ex.toString()); //NOSONAR
    });
    return flags;
}
}

5.) NoSqlRepository which persist meta data into NoSql DB.

@Component
public class NoSqlRepository {
private static final Logger LOGGER = LoggerFactory.getLogger(NoSqlRepository.class);
private final DocumentFormat documentFormat = DocumentFormat.JSON;
private static final String SEPARATOR = ",";

private static final ThreadLocal<MyLocalVariable> THREAD_LOCAL_VARIABLES = ThreadLocal.withInitial(() -> new MyLocalVariable());


static class MyLocalVariable {
    private JsonSchemaManager schemaManager = new JsonSchemaManager();
    private BasicBSONDecoder bsonDecoder = new BasicBSONDecoder();

    public JsonSchemaManager getSchemaManager() {
        return schemaManager;
    }

    public BasicBSONDecoder getBsonDecoder() {
        return bsonDecoder;
    }
}

private void checkSchemaIfAny(NoSqlEntity noSqlEntity, JsonNode entity) {
    if (noSqlEntity.isNotNullSchema()) {
        THREAD_LOCAL_VARIABLES.get().getSchemaManager().check(noSqlEntity, entity);
    }
}

public String saveEntity(NoSqlEntity noSqlEntity, JsonNode entity){
  // Before persisting payload into noSQL, validate payload against schema.
  this.checkSchemaIfAny(noSqlEntity,entity);
}
// Other CURD methods here...
}
๐ŸŒ
GitHub
github.com โ€บ networknt โ€บ json-schema-validator
GitHub - networknt/json-schema-validator: A fast Java JSON schema validator that supports draft V4, V6, V7, V2019-09 and V2020-12 ยท GitHub
This is a Java implementation of the JSON Schema Core Draft v4, v6, v7, v2019-09 and v2020-12 specification for JSON schema validation. This implementation supports Customizing Dialects, Vocabularies, Keywords and Formats.
Starred by 1K users
Forked by 338 users
Languages ย  Java 98.6% | Python 1.4%
๐ŸŒ
Medium
guillaumeblanchet.medium.com โ€บ three-ways-to-validate-json-in-java-4b38d95ba7c
Three Ways to Validate JSON in Java | by Guillaume Blanchet | Medium
May 9, 2021 - As you can see, you can use the oneOf combined schema to specify both possibilities. Then, you can use a json schema validation library like everit-org/json-schema or networknt/json-schema-validator to validate against your actual payload:
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ introduction to json schema in java
Introduction to JSON Schema in Java
November 27, 2024 - JSON Schema Validation: The JSON Schema Validation specification is the document that defines the valid ways to define validation constraints. This document also defines a set of keywords that can be used to specify validations for a JSON API.
๐ŸŒ
MS Charhag
mscharhag.com โ€บ java โ€บ json-schema-validation
JSON Schema validation in Java - Michael Scharhag
July 23, 2020 - In this post we will see how to ... in Java. We will use the same JSON document and Schema as in the previous post about JSON Schema. You can find both as text files on GitHub: JSON document and JSON Schema. We use the networknt JSON Schema validator library in this example. This library seems like a good fit because it supports the latest JSON Schema version (2019-09) and uses Jackson as JSON ...
๐ŸŒ
Restack
restack.io โ€บ p โ€บ java-validate-json-schema-jackson-answer
Java Validate Json Schema Jackson | Restackio
May 1, 2025 - JSON Schema is a powerful tool for validating the structure of JSON data. When working with Java, the Jackson library provides a robust way to handle JSON serialization and deserialization, as well as schema validation.
๐ŸŒ
GitHub
github.com โ€บ FasterXML โ€บ jackson-module-jsonSchema
GitHub - FasterXML/jackson-module-jsonSchema: (DEPRECATED for Jackson 3) Module for generating JSON Schema (v3) definitions from POJOs
This will yield a java pojo representing a JSON Schema, which can itself easily be serialized with jackson, or configured with java.
Starred by 385 users
Forked by 137 users
Languages ย  Java 98.4% | Logos 1.3% | RPC 0.3% | Java 98.4% | Logos 1.3% | RPC 0.3%
Top answer
1 of 2
17

I searched for the best practice to enforce validation for incoming json data into a RESTful service. My suggestion is to use a MessageBodyReader which performs the validation inside the readFrom method. Below there is an message-body-reader example which is non-generic for the sake of simplicity.

I also was interesed in finding the best framework for doing json data validation. Because I use the jackson framework (version 1.8.5) for marshaling and unmarshaling between json and java, it would have been nice if this framework would provide a json data validation functionality. Unfortunately I couldn't find any possibility to do this with jackson. Finally I got it working with the json-schema-validator available at https://github.com. The version I use is 2.1.7

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.servlet.ServletContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.map.ObjectMapper;

import at.fhj.ase.dao.data.Address;
import at.fhj.ase.xmlvalidation.msbreader.MessageBodyReaderValidationException;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.exceptions.ProcessingException;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
import com.github.fge.jsonschema.report.ProcessingReport;

@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class AddressJsonValidationReader implements MessageBodyReader<Address> {

    private final String jsonSchemaFileAsString;

    public AddressJsonValidationReader(@Context ServletContext servletContext) {
        this.jsonSchemaFileAsString = servletContext
                .getRealPath("/json/Address.json");
    }

    @Override
    public boolean isReadable(Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        if (type == Address.class) {
            return true;
        }
        return false;
    }

    @Override
    public Address readFrom(Class<Address> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
            throws IOException, WebApplicationException {

        final String jsonData = getStringFromInputStream(entityStream);
        System.out.println(jsonData);

        InputStream isSchema = new FileInputStream(jsonSchemaFileAsString);
        String jsonSchema = getStringFromInputStream(isSchema);

        /*
         * Perform JSON data validation against schema
         */
        validateJsonData(jsonSchema, jsonData);

        /*
         * Convert stream to data entity
         */
        ObjectMapper m = new ObjectMapper();
        Address addr = m.readValue(stringToStream(jsonData), Address.class);

        return addr;
    }

    /**
     * Validate the given JSON data against the given JSON schema
     * 
     * @param jsonSchema
     *            as String
     * @param jsonData
     *            as String
     * @throws MessageBodyReaderValidationException
     *             in case of an error during validation process
     */
    private void validateJsonData(final String jsonSchema, final String jsonData)
            throws MessageBodyReaderValidationException {
        try {
            final JsonNode d = JsonLoader.fromString(jsonData);
            final JsonNode s = JsonLoader.fromString(jsonSchema);

            final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            JsonValidator v = factory.getValidator();

            ProcessingReport report = v.validate(s, d);
            System.out.println(report);
            if (!report.toString().contains("success")) {
                throw new MessageBodyReaderValidationException(
                        report.toString());
            }

        } catch (IOException e) {
            throw new MessageBodyReaderValidationException(
                    "Failed to validate json data", e);
        } catch (ProcessingException e) {
            throw new MessageBodyReaderValidationException(
                    "Failed to validate json data", e);
        }
    }

    /**
     * Taken from <a href=
     * "http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/"
     * >www.mkyong.com</a>
     * 
     * @param is
     *            {@link InputStream}
     * @return Stream content as String
     */
    private String getStringFromInputStream(InputStream is) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();

        String line;
        try {

            br = new BufferedReader(new InputStreamReader(is));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }

    private InputStream stringToStream(final String str) throws UnsupportedEncodingException {
        return new ByteArrayInputStream(str.getBytes("UTF-8"));
    }

}
2 of 2
8
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jackson.JsonLoader;
import com.fasterxml.jackson.databind.JsonNode;

public class ValidationJSON {
    public static void main(String[] arr){
       String jsonData = "{\"name\": \"prem\"}";
       String jsonSchema = ""; //Schema we can generate online using http://jsonschema.net/
       final JsonNode data = JsonLoader.fromString(jsonData);
       final JsonNode schema = JsonLoader.fromString(jsonSchema);

       final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
       JsonValidator validator = factory.getValidator();

       ProcessingReport report = validator.validate(schema, data);
       System.out.println(report.isSuccess());
    }

}
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ watch
Why & How: JSON validation with JSON Schema and Jackson - YouTube
This quickie will briefly introduce you to JSON schema, explaining what you can gain (or not) by validating your JSON documents, and how to do so using the p...
Published ย  January 4, 2016
๐ŸŒ
Medium
medium.com โ€บ @mohommad.belal โ€บ json-schema-validator-using-java-springboot-667ed42480d5
JSON Schema Validator using Java SpringBoot. | Medium
May 11, 2024 - Simple json can be validated when mapped to POJOs or models using some constraints on the attributes. But, sometimes the json is complex and not all fields can be validated using such constraints.
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ data-validation-for-api-requests-in-spring-boot-with-json-schema-c109acbd325c
Data Validation for API Requests in Spring Boot with JSON Schema
August 16, 2025 - For JSON payloads, the built-in Jackson-based converter turns the raw text into Java objects. JSON Schema validation fits into this sequence by adding an extra verification step before ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ json-schema-support-using-jackson-in-java
JSON Schema Support using Jackson in Java?
July 8, 2020 - The JsonSchema can define validation, documentation, hyperlink navigation, and interaction control of JSON data. We can generate the JSON schema using the generateSchema() method of JsonSchemaGenerator, this class wraps the JSON schema generation functionality. public JsonSchema generateSc...
๐ŸŒ
GitHub
github.com โ€บ jthielens โ€บ json-schema
GitHub - jthielens/json-schema: Example of using Jackson for JSON and YAML parsing and json-schema-validator.
A JSON schema is of course itself ... can then be passed through schema_factory.getJsonSchema(JsonNode) to return a JsonSchema, which finally exposes a validate(JsonNode) method....
Forked by 2 users
Languages ย  Java 100.0% | Java 100.0%
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2022 โ€บ 12 โ€บ how-to-validate-json-in-java-jackson.html
How to validate JSON in Java Jackson - Example Tutorial
May 7, 2023 - If Jackson is able to parse a JSON document then it is a valid JSON, otherwise it's not. Alternatively, you can also use Postman tool to validate JSON using an external JSON Schema.
Top answer
1 of 2
2

The main scope of GSON and Jackson is to handle the serialization/deserialization of your data to/from JSON. Both the generation and validation of JSON Schemas are not their (main) concern.

As per comments on your other question (How to display default value in JSON Schema using Jackson), there are a couple of alternatives out there for the schema generation โ€“ the same applies to their validation.

List of Implementations (Generation & Validation)

As per R.Groote's answer, you can find some (but not all) of them on https://json-schema.org/implementations.html โ€“ both generation and validation libraries.

Validation

Personally, I've only used networknt/json-schema-validator so far and found it very intuitive and easy to use. It uses Jackson under the hood, which was practical in my use-case as I was already using the same library for the (de)serialization of data from/to JSON โ€“ so less additional dependencies.


Generation

Disclaimer: here comes the biased part as I'm the creator of victools/jsonschema-generator.

One example for a JSON Schema generation library would be victools/jsonschema-generator. That also uses Jackson internally, so again similar dependencies to the networknt/json-schema-validator.

The jsonschema-generator library does not populate the "default" value out-of-the-box, but you could easily configure it like this (based on the Jackson @JsonProperty annotation as per your other question):

SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(SchemaVersion.DRAFT_2019_09, OptionPreset.PLAIN_JSON);
configBuilder.forFields().withDefaultResolver(field -> {
    JsonProperty annotation = field.getAnnotationConsideringFieldAndGetter(JsonProperty.class);
    return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue());
});
SchemaGenerator generator = new SchemaGenerator(configBuilder.build());
JsonNode jsonSchema = generator.generateSchema(YourClass.class);

System.out.println(jsonSchema.toString());

If you are catering for numeric default values, you might have to include some conditional parsing of the @JsonProperty.defaultValue String then.

Additionally, if you want some more specific handling of Jackson annotations you could also consider adding the optional victools/jsonschema-module-jackson as well then.


EDIT (as response to your concerns having to define the default values twice โ€“ in the code and on the annotation):

With the victools/jsonschema-generator you have full control over where the default value is coming from. One possible approach could be to look-up the default values from actual instances of the respective objects being encountered โ€“ here filtering by your own package to skip external types.
The following is just a rough example of what is possible:

ConcurrentMap<Class<?>, Object> instanceCache = new ConcurrentHashMap<>();
configBuilder.forFields().withDefaultResolver(field -> {
    Class<?> declaringClass = field.getDeclaringType().getErasedType();
    if (!field.isFakeContainerItemScope()
            && declaringClass.getName().startsWith("your.package")) {
        MethodScope getter = field.findGetter();
        if (getter != null) {
            try {
                Object instance = instanceCache.computeIfAbsent(declaringClass, declaringClass::newInstance);
                Object defaultValue = getter.getRawMember().invoke(instance);
                return defaultValue;
            } catch (Exception ex) {
                // most likely missing a no-args constructor
            }
        }
    }
    return null;
});
2 of 2
1

I prefer to use GSON or Jackson: - https://www.baeldung.com/jackson-vs-gson

You could use Justify or json-schema-validator see below links: - https://json-schema.org/implementations.html - Validate JSON schema compliance with Jackson against an external schema file

I hope the above links give you enough information

๐ŸŒ
GitHub
github.com โ€บ everit-org โ€บ json-schema
GitHub - everit-org/json-schema: JSON Schema validator for java, based on the org.json API ยท GitHub
So here are some advices about which one to use: if you use Jackson to handle JSON in Java code, then java-json-tools/json-schema-validator is obviously a better choice, since it uses Jackson
Starred by 900 users
Forked by 279 users
Languages ย  Java
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ jackson-dev โ€บ c โ€บ vCtn7_FbZ6o
What to do with Jackson JSON Schema support?
Projects do exist which do a fine job of: * generating JSON Schemas from Java classes: JJSchema; * generating Java classes from JSON Schemas: jsonschema2pojo (draft v3 only though). And there is of course JSON Schema validation which my project does. It so happens that all of them use Jackson!
๐ŸŒ
MS Charhag
mscharhag.com โ€บ spring โ€บ json-schema-validation-handlermethodargumentresolver
Integrating JSON Schema validation in Spring using a custom HandlerMethodArgumentResolver
Within our implementation we validate the JSON request body against a JSON Schema before it is passed as argument to the controller. To return proper error message we can add a @ExceptionHandler method. You can find the complete example project on GitHub. ... Excellent example.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ programmatic generation of json schemas in java
Programmatic Generation of JSON Schemas in Java | Baeldung
July 30, 2024 - This module derives JSON Schema attributes from Jackson annotations. Continuing, weโ€™ll install jsonschema-module-jakarta-validation to get the schema from Jakarta validation annotations: