Is there a tool to generate a JSON schema from an XML schema through Java?

29,004

Solution 1

It isn't very elegant, but jackson can generate json schema from a java class . So you could take your xml schema, generate java classes from it with jaxb annotations, then generate the json schema from that as jackson supports jaxb annotations.

Solution 2

If you can get POJOs that match Schema (using xjc for example), you could then use Jackson to produce JSON Schema (see ObjectMapper.generateSchema(...)).

Solution 3

I'd expect you to be able to write an XSLT script that generated JSON structures from the XML schema fairly easily. This works because XSLT is prefectly happy to read and transform XML, and an XML schema is just XML.

Going the other way would be a bit harder. You need somthing that can read a JSON schema and spit out XML. For this you need a parser, and likely something that builds an AST of the parse. With that and a tree walk you're likely to be able to generate an XML schema fairly easily.

ANTLR will let you define grammars, build a parser and an AST fairly easily. It has some kind of "Structured Text" generator that might work for walking the AST.

Our DMS Software Reengineering Toolkit is like ANTLR but with more machinery. With DMS you can define the JSON syntax, build ASTs, and then write source-to-source tranformatinos to map that to XML representing your schema.

Solution 4

for example CXF can serve request in Json or XML so it must have a way to convert the two.

Solution 5

Converting XML to JSON is quite easy and can be done various ways:

http://answers.oreilly.com/topic/278-how-to-convert-xml-to-json-in-java
http://www.json.org/javadoc/org/json/XML.html#toJSONObject%28java.lang.String%29

For converting from XML to JSON look at this maybe, seems to be very simple:

http://www.bramstein.com/projects/xsltjson/
http://code.google.com/p/xml2json-xslt/
http://json-lib.sourceforge.net/index.html

There is also http://x-stream.github.io/ library which allows you to make conversion in both sides (also to POJOs). Simple example usage you can find here: Convert XML to JSON format

/edit: ups, looks like I didn't understood question correctly :P

Share:
29,004
Harish Raj
Author by

Harish Raj

Updated on July 09, 2022

Comments

  • Harish Raj
    Harish Raj almost 2 years

    Is anyone aware of a tool or approach from which we can generate a JSON schema from XML schema or XML schema from JSON schema by Java?

  • sbridges
    sbridges over 12 years
    json schema is json, so you only need a json parser
  • StaxMan
    StaxMan over 12 years
    And XML to JSON is not very valuable either, because it tends to produce "franken-JSON" which contains unnecessary non-JSON stuff (to retain XML namespace info, attribute/element distinction etc). Automated conversion could be consider an anti-pattern or code smell.
  • StaxMan
    StaxMan over 12 years
    No, this not necessarily mean that conversion goes through schemas, or that there even is a conversion between XML and JSON. Many frameworks instead produce XML and JSON separately, as separate representations. It is easier to convert between POJOs and JSON than between XML and JSON.
  • Ira Baxter
    Ira Baxter over 9 years
    OP's original question was how to generate json from XML.