Gson doesn't have a JSON schema validation feature to specify that a particular element must be present, and it doesn't have a way to specify that a Java member must be populated. It might be nice to have such a feature available, such as with an @Required annotation. Head on over to the Gson Issues List and put in an enhancement request.

With Gson, you could enforce that specified JSON elements are present with a custom deserializer.

// output: 
//   [MyObject: element1=value1, element2=value2, element3=value3]
//   [MyObject: element1=value1, element2=value2, element3=null]
//   Exception in thread "main" com.google.gson.JsonParseException: Required Field Not Found: element2

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class Foo
{
  static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
  static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
  static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    MyDeserializer deserializer = new MyDeserializer();
    deserializer.registerRequiredField("element2");
    gsonBuilder.registerTypeAdapter(MyObject.class, deserializer);
    Gson gson = gsonBuilder.create();
    MyObject object1 = gson.fromJson(jsonInput1, MyObject.class);
    System.out.println(object1);
    MyObject object2 = gson.fromJson(jsonInput2, MyObject.class);
    System.out.println(object2);
    MyObject object3 = gson.fromJson(jsonInput3, MyObject.class);
    System.out.println(object3);
  }
}

class MyObject
{
  String element1;
  String element2;
  String element3;

  @Override
  public String toString()
  {
    return String.format(
        "[MyObject: element1=%s, element2=%s, element3=%s]",
        element1, element2, element3);
  }
}

class MyDeserializer implements JsonDeserializer<MyObject>
{
  List<String> requiredFields = new ArrayList<String>();

  void registerRequiredField(String fieldName)
  {
    requiredFields.add(fieldName);
  }

  @Override
  public MyObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException
  {
    JsonObject jsonObject = (JsonObject) json;
    for (String fieldName : requiredFields)
    {
      if (jsonObject.get(fieldName) == null)
      {
        throw new JsonParseException("Required Field Not Found: " + fieldName);
      }
    }
    return new Gson().fromJson(json, MyObject.class);
  }
}

A preferable approach might be to use an API that provides JSON Schema validation. Jackson has at least a rudimentary implementation available. JSON Tools looks to have a more mature one.

Here's an example with Jackson.

// output: 
// Validating jsonInput1...
// Validating jsonInput2...
// Validating jsonInput3...
// $.element2: is missing and it is not optional
// [MyObject: element1=value1, element2=value2, element3=value3]
// [MyObject: element1=value1, element2=value2, element3=null]
// [MyObject: element1=value1, element2=null, element3=value3]

import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import eu.vahlas.json.schema.JSONSchema;
import eu.vahlas.json.schema.JSONSchemaProvider;
import eu.vahlas.json.schema.impl.JacksonSchemaProvider;

public class Foo
{
  static String jsonSchema = 
    "{" + 
        "\"description\":\"Serialized MyObject Specification\"," + 
        "\"type\":[\"object\"]," + 
        "\"properties\":" + 
        "{" + 
            "\"element1\":{\"type\":\"string\"}," + 
            "\"element2\":{\"type\":\"string\",\"optional\":false}," + 
            "\"element3\":{\"type\":\"string\",\"optional\":true}" + 
        "}" + 
    "}";;

  static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
  static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
  static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";

  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);
    JSONSchema schema = schemaProvider.getSchema(jsonSchema);

    System.out.println("Validating jsonInput1...");
    validateAndLogErrors(jsonInput1, schema);
    System.out.println("Validating jsonInput2...");
    validateAndLogErrors(jsonInput2, schema);
    System.out.println("Validating jsonInput3...");
    validateAndLogErrors(jsonInput3, schema);

    MyObject object1 = mapper.readValue(jsonInput1, MyObject.class);
    System.out.println(object1);
    MyObject object2 = mapper.readValue(jsonInput2, MyObject.class);
    System.out.println(object2);
    MyObject object3 = mapper.readValue(jsonInput3, MyObject.class);
    System.out.println(object3);
  }

  static void validateAndLogErrors(String jsonInput, JSONSchema schema)
  {
    List<String> errors = schema.validate(jsonInput);
    for (String error : errors)
    {
      System.out.println(error);
    }
  }
}

class MyObject
{
  String element1;
  String element2;
  String element3;

  void setElement1(String element1)
  {
    this.element1 = element1;
  }

  void setElement2(String element2)
  {
    this.element2 = element2;
  }

  void setElement3(String element3)
  {
    this.element3 = element3;
  }

  @Override
  public String toString()
  {
    return String.format(
        "[MyObject: element1=%s, element2=%s, element3=%s]",
        element1, element2, element3);
  }
}
Answer from Programmer Bruce on Stack Overflow
Top answer
1 of 2
20

Gson doesn't have a JSON schema validation feature to specify that a particular element must be present, and it doesn't have a way to specify that a Java member must be populated. It might be nice to have such a feature available, such as with an @Required annotation. Head on over to the Gson Issues List and put in an enhancement request.

With Gson, you could enforce that specified JSON elements are present with a custom deserializer.

// output: 
//   [MyObject: element1=value1, element2=value2, element3=value3]
//   [MyObject: element1=value1, element2=value2, element3=null]
//   Exception in thread "main" com.google.gson.JsonParseException: Required Field Not Found: element2

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class Foo
{
  static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
  static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
  static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";

  public static void main(String[] args)
  {
    GsonBuilder gsonBuilder = new GsonBuilder();
    MyDeserializer deserializer = new MyDeserializer();
    deserializer.registerRequiredField("element2");
    gsonBuilder.registerTypeAdapter(MyObject.class, deserializer);
    Gson gson = gsonBuilder.create();
    MyObject object1 = gson.fromJson(jsonInput1, MyObject.class);
    System.out.println(object1);
    MyObject object2 = gson.fromJson(jsonInput2, MyObject.class);
    System.out.println(object2);
    MyObject object3 = gson.fromJson(jsonInput3, MyObject.class);
    System.out.println(object3);
  }
}

class MyObject
{
  String element1;
  String element2;
  String element3;

  @Override
  public String toString()
  {
    return String.format(
        "[MyObject: element1=%s, element2=%s, element3=%s]",
        element1, element2, element3);
  }
}

class MyDeserializer implements JsonDeserializer<MyObject>
{
  List<String> requiredFields = new ArrayList<String>();

  void registerRequiredField(String fieldName)
  {
    requiredFields.add(fieldName);
  }

  @Override
  public MyObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
      throws JsonParseException
  {
    JsonObject jsonObject = (JsonObject) json;
    for (String fieldName : requiredFields)
    {
      if (jsonObject.get(fieldName) == null)
      {
        throw new JsonParseException("Required Field Not Found: " + fieldName);
      }
    }
    return new Gson().fromJson(json, MyObject.class);
  }
}

A preferable approach might be to use an API that provides JSON Schema validation. Jackson has at least a rudimentary implementation available. JSON Tools looks to have a more mature one.

Here's an example with Jackson.

// output: 
// Validating jsonInput1...
// Validating jsonInput2...
// Validating jsonInput3...
// $.element2: is missing and it is not optional
// [MyObject: element1=value1, element2=value2, element3=value3]
// [MyObject: element1=value1, element2=value2, element3=null]
// [MyObject: element1=value1, element2=null, element3=value3]

import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import eu.vahlas.json.schema.JSONSchema;
import eu.vahlas.json.schema.JSONSchemaProvider;
import eu.vahlas.json.schema.impl.JacksonSchemaProvider;

public class Foo
{
  static String jsonSchema = 
    "{" + 
        "\"description\":\"Serialized MyObject Specification\"," + 
        "\"type\":[\"object\"]," + 
        "\"properties\":" + 
        "{" + 
            "\"element1\":{\"type\":\"string\"}," + 
            "\"element2\":{\"type\":\"string\",\"optional\":false}," + 
            "\"element3\":{\"type\":\"string\",\"optional\":true}" + 
        "}" + 
    "}";;

  static String jsonInput1 = "{\"element1\":\"value1\",\"element2\":\"value2\",\"element3\":\"value3\"}";
  static String jsonInput2 = "{\"element1\":\"value1\",\"element2\":\"value2\"}";
  static String jsonInput3 = "{\"element1\":\"value1\",\"element3\":\"value3\"}";

  public static void main(String[] args) throws Exception
  {
    ObjectMapper mapper = new ObjectMapper();
    JSONSchemaProvider schemaProvider = new JacksonSchemaProvider(mapper);
    JSONSchema schema = schemaProvider.getSchema(jsonSchema);

    System.out.println("Validating jsonInput1...");
    validateAndLogErrors(jsonInput1, schema);
    System.out.println("Validating jsonInput2...");
    validateAndLogErrors(jsonInput2, schema);
    System.out.println("Validating jsonInput3...");
    validateAndLogErrors(jsonInput3, schema);

    MyObject object1 = mapper.readValue(jsonInput1, MyObject.class);
    System.out.println(object1);
    MyObject object2 = mapper.readValue(jsonInput2, MyObject.class);
    System.out.println(object2);
    MyObject object3 = mapper.readValue(jsonInput3, MyObject.class);
    System.out.println(object3);
  }

  static void validateAndLogErrors(String jsonInput, JSONSchema schema)
  {
    List<String> errors = schema.validate(jsonInput);
    for (String error : errors)
    {
      System.out.println(error);
    }
  }
}

class MyObject
{
  String element1;
  String element2;
  String element3;

  void setElement1(String element1)
  {
    this.element1 = element1;
  }

  void setElement2(String element2)
  {
    this.element2 = element2;
  }

  void setElement3(String element3)
  {
    this.element3 = element3;
  }

  @Override
  public String toString()
  {
    return String.format(
        "[MyObject: element1=%s, element2=%s, element3=%s]",
        element1, element2, element3);
  }
}
2 of 2
0

You can recursively verify whether the json contains fields that are not declared in the class :

 private static List<String> verifyElement(JsonObject element, Class klass) throws NoSuchFieldException, IllegalAccessException {
  List<String> unknownFields = new ArrayList<>();
  Set<String> classFields = new HashSet<>();

  for (Field field : klass.getDeclaredFields()) {
    if (!Modifier.isPublic(field.getModifiers())) {
      throw new IllegalArgumentException("All fields must be public. Please correct this field :" + field);
    }
  }

  for (Field field : klass.getFields()) {
    classFields.add(field.getName());
  }

  // Verify recursively that the class contains every
  for (Map.Entry<String, JsonElement> entry : element.entrySet()) {
    if (!classFields.contains(entry.getKey())) {
      unknownFields.add(klass.getCanonicalName() + "::" + entry.getKey() + "\n");
    } else {
      Field field = klass.getField(entry.getKey());
      Class fieldClass = field.getType();
      if (!fieldClass.isPrimitive() && entry.getValue().isJsonObject()) {
        List<String> elementErrors = verifyElement(entry.getValue().getAsJsonObject(), fieldClass);
        unknownFields.addAll(elementErrors);
      }
    }
  }
  return unknownFields;

}
🌐
GitHub
github.com › google › gson › issues › 783
Validate JSon again Json Schema file · Issue #783 · google/gson
February 4, 2016 - Unfortunately there is no good Java Json Schema validator. Neither in Gson nor in any 3rd party library.
🌐
Baeldung
baeldung.com › home › json › introduction to json schema in java
Introduction to JSON Schema | Baeldung
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.
🌐
Inductiveautomation
files.inductiveautomation.com › sdk › javadoc › ignition80 › 8.0.0-beta › com › inductiveautomation › ignition › common › jsonschema › JsonSchema.html
JsonSchema
Validate the given JsonElement, the given node is the child node of the root node at given data path. ... A list of ValidationMessage if there is any validation error, or an empty list if there is no error. ... If the schema specified a default value, it will be returned here.
🌐
GitHub
github.com › google › gson › issues › 17
Json + schema · Issue #17 · google/gson
March 19, 2015 - In Gson, currently, the Java classes define the schema. May be we can provide a validator that validates the schema against a set of classes.Or generates the Json schema corresponding to a set of classes (using the ObjectNavigator).
Author   GoogleCodeExporter
🌐
Restack
restack.io › p › java-validate-json-schema-gson-answer-databricks-best-practices
Java Validate Json Schema Gson | Restackio
April 23, 2025 - To validate JSON against a schema in Java using Gson, you can leverage the json-schema-validator library alongside Gson.
🌐
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:
🌐
Readthedocs
validol.readthedocs.io › en › latest › faq › how_to_parse_and_validate_json_schema_in_java.html
How to validate json schema in Java? — Validol documentation
There are at least a couple of great libraries that do that: Jackson and GSON. The problem is that there seems to be no nice way to combine them with validation facility. That’s where the following problems unfold. Typically, json schema validation looks like a huge, highly temporally coupled ...
Find elsewhere
🌐
GitHub
github.com › everit-org › json-schema
GitHub - everit-org/json-schema: JSON Schema validator for java, based on the org.json API · GitHub
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 · if you want to use the org.json API then this library is the better choice · if you need JSON Schema Draft 6 / 7 support, then you need this library. if you want to use anything else for handling JSON (like GSON or javax.json), then you are in a little trouble, since currently there is no schema validation library backed by these libraries.
Starred by 900 users
Forked by 279 users
Languages   Java
Top answer
1 of 7
13

I found solution but using org.json library, according to How to check whether a given string is valid JSON in Java

public static boolean isJson(String Json) {
        try {
            new JSONObject(Json);
        } catch (JSONException ex) {
            try {
                new JSONArray(Json);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }

Now random looking string bncjbhjfjhj is false and {"status": "UP"} is true.

2 of 7
12

You should not use Gson to make such validation:

  • Gson is an object that performs deserialization therefore it deserializes entire JSON as an object in memory.
  • Gson, and I didn't know it, may be not very strict for some invalid JSONs: bncjbhjfjhj is deserialized as a java.lang.String instance. Surprise-surprise!
private static final Gson gson = new Gson();

private static final String VALID_JSON = "{\"status\": \"UP\"}";
private static final String INVALID_JSON = "bncjbhjfjhj";

System.out.println(gson.fromJson(VALID_JSON, Object.class).getClass());
System.out.println(gson.fromJson(INVALID_JSON, Object.class).getClass());

Output:

class com.google.gson.internal.LinkedTreeMap
class java.lang.String

What you can do here is using JsonReader to read incoming JSON token by token thus making if the given JSON document is syntactically valid.

private static boolean isJsonValid(final String json)
        throws IOException {
    return isJsonValid(new StringReader(json));
}

private static boolean isJsonValid(final Reader reader)
        throws IOException {
    return isJsonValid(new JsonReader(reader));
}

private static boolean isJsonValid(final JsonReader jsonReader)
        throws IOException {
    try {
        JsonToken token;
        loop:
        while ( (token = jsonReader.peek()) != END_DOCUMENT && token != null ) {
            switch ( token ) {
            case BEGIN_ARRAY:
                jsonReader.beginArray();
                break;
            case END_ARRAY:
                jsonReader.endArray();
                break;
            case BEGIN_OBJECT:
                jsonReader.beginObject();
                break;
            case END_OBJECT:
                jsonReader.endObject();
                break;
            case NAME:
                jsonReader.nextName();
                break;
            case STRING:
            case NUMBER:
            case BOOLEAN:
            case NULL:
                jsonReader.skipValue();
                break;
            case END_DOCUMENT:
                break loop;
            default:
                throw new AssertionError(token);
            }
        }
        return true;
    } catch ( final MalformedJsonException ignored ) {
        return false;
    }
}

And then test it:

System.out.println(isJsonValid(VALID_JSON));
System.out.println(isJsonValid(INVALID_JSON));

Output:

true
false

🌐
Stack Overflow
stackoverflow.com › questions › 22806800 › json-string-validation-with-googles-gson
java - JSON string validation with Google's Gson? - Stack Overflow
I have to exposed a REST servivce, ... string confirms the json schema. If input json string confirms the schmea then i have to create a asynch thread which will process the json data. ... Json string validation against json scheam using Google's Gson....
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to validate your json using json schema
How to Validate Your JSON Using JSON Schema | Towards Data Science
January 21, 2025 - It enables you to validate your JSON structure and make sure it meets the required API. You can create a schema as complex and nested as you need, all you need are the requirements.
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

🌐
javathinking
javathinking.com › blog › how-to-check-if-json-is-valid-in-java-using-gson
How to Validate JSON in Java Using GSON: Fixing the 'Always Returns True' Issue — javathinking.com
... Syntaxically correct: Follows JSON’s structural rules (e.g., proper use of braces, commas, quotes). Semantically valid: Conforms to a predefined schema or data model (e.g., required fields exist, data types match expectations like numbers ...
🌐
GitHub
google.github.io › gson › UserGuide.html
Gson User Guide | gson
It also has an annotation based strategy to allows clients to define custom names on a per field basis. Note, that the annotation based strategy has field name validation which will raise “Runtime” exceptions if an invalid field name is provided as the annotation value. The following is an example of how to use both Gson naming policy features:
🌐
GitHub
github.com › worldturner › medeia-validator
GitHub - worldturner/medeia-validator: Medeia JSON-Schema Validator
It includes examples on how to read and write using Jackson and Gson while also loading or retrieving from Java/Kotlin objects. The CustomFormatExample also doubles as a way to show how streaming validation can be done without loading the data into memory at all. The allows medeia-validator to validate many Gigabytes of data. The MedeiaJacksonApi and MedeiaGsonApi classes have various methods to load schemas and to create validating parsers/generators (or readers/writers in Gson parlance)
Starred by 58 users
Forked by 11 users
Languages   Kotlin 95.0% | Java 5.0% | Kotlin 95.0% | Java 5.0%
🌐
Stack Overflow
stackoverflow.com › questions › 38503066 › parsing-json-schema-using-gson-without-predefined-object-in-java
Parsing JSON SCHEMA using GSON without predefined object in java - Stack Overflow
May 24, 2017 - ); } } else { if ( !JsonSchemaTagEnum.TYPE.name().equalsIgnoreCase( entry.getKey() ) ) { if ( JsonSchemaTagEnum.MIN_ITEMS.name().equalsIgnoreCase( entry.getKey() ) ) { if ( entry.getValue().isJsonPrimitive() ) { parentlement.setMinItems( entry.getValue().getAsString() ); } else if ( entry.getValue().isJsonNull() ) { parentlement.setMinItems( entry.getValue().getAsJsonNull().toString() ); } } else if ( JsonSchemaTagEnum.MAX_ITEMS.name().equalsIgnoreCase( entry.getKey() ) ) { if ( entry.getValue().isJsonPrimitive() ) { parentlement.setMaxItems( entry.getValue().getAsString() ); } else if ( entry
🌐
JSON Schema Validator
jsonschemavalidator.net
JSON Schema Validator - Newtonsoft
public class JsonSchemaController : ControllerBase { [HttpPost] [Route("api/jsonschema/validate")] public ValidateResponse Validate(ValidateRequest request) { // Load schema JSchema schema = JSchema.Parse(request.Schema); JToken json = JToken.Parse(request.Json); // Validate json IList<ValidationError> errors; bool valid = json.IsValid(schema, out errors); // Return error messages and line info to the browser return new ValidateResponse { Valid = valid, Errors = errors }; } } public class ValidateRequest { public string Json { get; set; } public string Schema { get; set; } } public class ValidateResponse { public bool Valid { get; set; } public IList<ValidationError> Errors { get; set; } }