2

I'm defining the structure of JSON documents. I'd like to know if how to validate the JSON documents against existing XSD, RelaxNG schemas or another standard schema language schema. I know of Jsonix, but I don't see that it uses the regular expressions from XSD or RelaxNG for validation against JSON schema (and I don't think that JSON schema is standardized).

Clarification: We already have existing XML and XSD. We can always go back to XML for validation, but it would be cool if we could validate the JSON directly, and would give us more confidence when we want to use the JSON and not XML.

Update: Here is the specification in question: http://www.web3d.org/specifications/x3d-3.4.xsd note that it doesn't have text nodes in the XML documents.

Preliminary answer (still a work in progress, but you can contribute): If you want to convert XML schema to JSON schema, try downloading: XSD2OWL stylesheet which converts XML Schema to OWL. I converted my schema to owl like this:

    $ xmlsh
    $ xslt -f xsd2owl.xsl -cf file.xsd > file.owl
    $ exit

Then download owl2jsonschema.js ** NO LICENSE ** and modify it until it until the demo works. The output will be in demo/OUTPUT/schema folder/*.json as separate JSON files.

John Carlson
  • 321
  • 1
  • 13
  • And I don't think I should have to convert the JSON to XML first. – John Carlson Oct 31 '15 at 19:24
  • If you want to convert XML schema to JSON schema, which is very rough at this point and won't work without changes, try downloading: [link](http://rhizomik.net/redefer-services/xsl/xsd2owl.xsl) which converts XML Schema to OWL. I converted my schema to owl like this: ` $ xmlsh $ xslt -f xsd2owl.xsl -cf x3d-3.4.xsd > x3d-3.4.owl $ exit ` Then download [link](https://github.com/redaktor/owl2jsonschema.js.git) and modify it until it until the demo works. The output will be in demo/OUTPUT. – John Carlson Nov 05 '15 at 08:23

2 Answers2

2

XSD and RelaxNG are defined against XML, not JSON.

For JSON, see JSON Schema, but realize that it has nowhere near the adoption of XSD, and the latest draft of the specification expired August 3, 2013, casting doubts on the future of the effort.


Update

How do I validate JSON against XML Schema (XSD) or RelaxNG?

You don't.

The question is not "Can I?" but "How?" Say I have total control over the JSON document.

When the answer to "Can I?" is "No" the question of how does not apply.

Clarification: We already have existing XML and XSD. We can always go back to XML for validation, but it would be cool if we could validate the JSON directly, and would give us more confidence when we want to use the JSON and not XML.

You can validate the JSON directly against a JSON Schema, but not against an XSD. There are no tools that can do that; the standards are substantially different. The need to define standard vocabularies and grammars that is served by XSD and RelaxNG against XML was intended to be met by JSON Schema against JSON.

You're looking for "confidence when we want to use the JSON and not XML" in the wrong place. See reasons for choosing XML vs JSON instead.

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • The question is not "Can I?" but "How?" Say I have total control over the JSON document. – John Carlson Oct 31 '15 at 21:26
  • Please see updated answer for responses to your clarifications/follow-ups. Thanks. – kjhughes Nov 01 '15 at 00:47
  • It would make sense if I can create a JSON schema from XSD, and I can create JSON from XML that follows the XSD, and I can validate the JSON with the JSON schema, that you can validate the JSON with the XSD. See Jsonix. I know it's possible. Don't say I can't, because I will prove you wrong. Basically, I'm trying to find out if anyone has attempted it and what the results were. – John Carlson Nov 01 '15 at 01:02
  • In the simplest case, I can convert the JSON to XML and validate it against the XSD that way. I just want to skip the conversion to XML step. – John Carlson Nov 01 '15 at 01:18
  • Re: "See reasons for choosing XML vs JSON instead." We are defining a JSON standard for VRML. There already is an XML standard. We want to use JSON too. – John Carlson Nov 01 '15 at 01:26
  • The JSON schema is 5 times bigger than the RelaxNG one. I don't think it's feasible that I maintain a JSON schema, so it's either generate a JSON schema and use it, or use a RelaxNG or XSD schema. I don't see the point in creating a JSON schema when I can create JSON documents that are essentially XML-like and then validate against XSD. I know it's possible. What's the best way? I'm talking performance-wise, because I may be doing this in an interpreted language on large documents, so I don't want to convert to XML first, unless it's faster. – John Carlson Nov 01 '15 at 01:31
  • My time is an issue so the question is not "Can I?", but "Can someone?" and "How did you do it?" – John Carlson Nov 01 '15 at 02:02
  • Your original question acknowledged Jsonix but went on to ask about validating JSON *directly* via XSD/RelaxNG, so I steered away from Jsonix and translation-based approaches. Anyway, sorry you didn't find this helpful. Good luck with your project. – kjhughes Nov 01 '15 at 02:46
  • Thank you for your answer. In the short term, I will validate against JSON schema with Jsonix and Ajv?, or translate to XML. This is not the correct forum to bring up design ideas. Sorry. I did do searches, but couldn't find anything. I guess no one has worked on it. There is a relax json github repository, but just discussions there. – John Carlson Nov 01 '15 at 19:12
  • So it looks like we're moving towards an XML/JSON solution which defines our object model. XML documents are still validated against an XSD, but the XSD is used to generate an object model. I have generated JSON schema from this object model, but it's beginning to seem a lot simpler to validate directly against the object model (if the object model is in XML or JSON, I don't really care it seems). Better yet, the object model is being converted to Java, which can do checks beyond typical schema checks. I just have to get my JSON into Java, which means converting to DOM! Bosses say WTF? – John Carlson Oct 22 '18 at 09:23
  • Note that expiration of an Internet Draft doesn’t by itself mean much. Drafts auto-expire after a few months and it just means there hasn’t been a newer draft. – Anton Strogonoff Jun 12 '23 at 08:09
0

As you're probably already aware of the information I'm about to post, this is just for reference.

Jsonix Schema Compiler supports generation of JSON Schema based on the XML Schema.

So with this you can convert you XML Schema into JSON Schema and validate your JSON against this JSON Schema using AJV.

This is still an experimental feature but that's the direction.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Yep, see above comments. That's what this whole thing is about at this point. I am using Jsonix, but so far, it doesn't support regular expressions. I have an issue in on github with this specific request. I know about JSR-303 after more research. Another thing I don't like about Jsonix is that thrashing the code takes after each schema compile. My mappings and JSON schema almost always change. What I plan to do is generate the schema and then use it as a basis for hand-edited future modifications. Jsonix will get better, just have to wait and check later. – John Carlson Nov 03 '15 at 14:12
  • We are currently converting the XML to JSON as a basis for the JSON standard. We have an XSLT solution, a Java SAX solution and a Jsonix solution. Jsonix looks a bit weak as the XML generated seems to grab strings at seeming randomness from the schema and it sticks them into the JSON. The XSD schema is http://www.web3d.org/specifications/x3d-3.4.xsd if you want to see it. – John Carlson Nov 03 '15 at 14:14