Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate JSON against XML Schema (XSD)

Is it possible to validate JSON with an XSD in Java? I have an application where I receive JSON response, and I would like to validate it against existing XSD. Another part of my application uses XML, which is why it would be easiest if they both could validate against the existing XSD.

like image 661
user16655 Avatar asked Mar 22 '16 11:03

user16655


People also ask

How do you validate a JSON file against a schema?

The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.

Can XSD be used for JSON?

The XSD type converts a JSON string to an XML document defined using a schema specified in the Output Editor. The user may want to use the Generic type a specific schema is not required for conversion or the XSD type can be used when conversion needs to be done based on a particular schema.

How do I know if my XSD is valid?

Validate XML documents XML documents are validated by the Create method of the XmlReader class. To validate an XML document, construct an XmlReaderSettings object that contains an XML schema definition language (XSD) schema with which to validate the XML document.


1 Answers

No, XML Schema (XSD) is for validating XML; to validate JSON, see JSON Schema.

I recommend generating schemas by hand for full understanding and full control over the constraints. However, here are some automated tools that can jumpstart the process:

  • To convert from JSON Schema to XSD, see jsons2xsd.
  • To convert from XSD to JSON Schema, see Jsonix Schema Compiler.

Related and also very useful:

  • To parse from XML to JSON (unmarshal) or serialize JSON to XML (marshal), see JSONIX.
  • For a list of implementations, including validators in various languages, see JSON-Schema Implementations.
like image 61
kjhughes Avatar answered Sep 17 '22 15:09

kjhughes