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.
Asked
Active
Viewed 3.1k times
18
-
You'd have to convert it to xml i think... urgh. Just use JSON everywhere :-) – mbx-mbx Mar 22 '16 at 11:21
-
2for json it's better to generate a json shema similar to xsd and validate against it. There are third-party libs to validate json against schema, maybe exists such for Java. – Alex Salauyou Mar 22 '16 at 11:35
2 Answers
25
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.

kjhughes
- 106,133
- 27
- 181
- 240
-
-
Generally, sure. See *Validators* section of [JSON-Schema Implementations](http://json-schema.org/implementations.html). – kjhughes Mar 29 '16 at 10:55
-
Can you expand on why you can't use XSD for JSON? I imagine you could just convert the JSON to XML then check that XML against the XSD. Other than raw text in XML, JSON and XML are just elements which have attributes and child elements. – ubiquibacon Jun 07 '19 at 21:09
-
@ubiquibacon: Of course any XML can be validated with XSD, including XML that was automatically converted from JSON using the methods I mentioned in the answer. – kjhughes Jun 08 '19 at 02:27
3
No, the standards are different between the two.
But if you really want to rely on the xsd for validating, Jsonix Schema Compiler can help you generate a JSON Schema to validate your json from your XML Schema.

ingrid.e
- 531
- 2
- 11