jsons2xsd

Maven Central License: MIT Coverage Status Codacy Badge Build Status GitHub issues open

JSON-schema to XML schema converter written in Java.

Dependency

<dependency>
  <groupId>com.ethlo.jsons2xsd</groupId>
  <artifactId>jsons2xsd</artifactId>
  <version>2.2.0</version>
</dependency>

Snapshots

<repositories>
  <repository>
    <id>sonatype-nexus-snapshots</id>
    <snapshots>
      <enabled>true</enabled>
    </snapshots>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
  </repository>
</repositories>

Usage

try (final Reader r = ...)
{
  final Config cfg = new Config.Builder()
    .targetNamespace("http://example.com/myschema.xsd")
    .name("array")
    .build();
  final Document doc = Jsons2Xsd.convert(r, cfg);
}

Example input

{
  "type":"array",
  "items":{
    "type":"object",
    "properties":{
      "price":{
        "type":"number",
        "minimum":0
      },
      "name":{
        "type":"string",
        "minLength":5,
        "maxLength":32
      },
      "isExpired":{
        "default":false,
        "type":"boolean"
      },
      "manufactured":{
        "type":"string",
        "format":"date-time"
      }
    },
    "required":[
      "price",
      "name",
      "manufactured"
    ]
  }
}

Example output

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:x="http://example.com/myschema.xsd" elementFormDefault="qualified" targetNamespace="http://example.com/myschema.xsd">
  <complexType name="array">
    <sequence>
      <element name="price">
        <simpleType>
          <restriction base="decimal">
            <minInclusive value="0" />
          </restriction>
        </simpleType>
      </element>
      <element name="name">
        <simpleType>
          <restriction base="string">
            <minLength value="5" />
            <maxLength value="32" />
          </restriction>
        </simpleType>
      </element>
      <element minOccurs="0" name="isExpired" type="boolean" />
      <element name="manufactured" type="dateTime" />
    </sequence>
  </complexType>
</schema>

Support for non-standard types and formats

Ignore unknown JSON formats

final Config cfg = new Config.Builder()
    .ignoreUnknownFormats(true)
    ...
    .build();

Register custom JSON formats

final Config cfg = new Config.Builder()
    .customTypeMapping(JsonSimpleType.INTEGER, "int64", XsdSimpleType.LONG)
    .customTypeMapping(JsonSimpleType.INTEGER, "int32", XsdSimpleType.INT)
    .customTypeMapping(JsonSimpleType.STRING, "ext-ref", XsdSimpleType.STRING)
    ...
    .build();

Register non-JSON types

final Config cfg = new Config.Builder()
    .nonJsonTypeMapping("date-time", XsdSimpleType.DATE_TIME)
    .nonJsonTypeMapping("int", XsdSimpleType.INT)
    ...
    .build();