Reputation: 687
I have the below json , how can it be represented in XSD. It is a json tuple i could not find a valid construct in XSD to represent this type of json structure.
{
"type": "array",
"items": [
{
"type": "number"
},
{
"type": "string"
},
{
"type": "string",
"enum": ["Street", "Avenue", "Boulevard"]
},
{
"type": "string",
"enum": ["NW", "NE", "SW", "SE"]
}
]
}
Upvotes: 2
Views: 7461
Reputation: 21638
It would help others if you would provide more context to your question. First, your example is from page 30 (print version) of the Understanding JSON Schema guide. Your JSON is a JSON schema (draft #4).
Because you're talking about schemas, I take your question as being about models. To be even clearer, I consider your question similar to one asked many years back: can one use UML to represent an XSD structure?
I actually do use XSD to describe JSON structures; we have an automatic conversion between XSD and JSON schemas (draft #4), since I use myself XSD as a data modeling language.
Your particular example has no natural XSD equivalent. By natural, I mean one that would make sense as XML as well, and which a "generic" XML-to-JSON transformation would yield an expected result.
If we take away simple things, such as a simple type modeled as a list:
<xsd:simpleType name="array">
<xsd:list itemType="xsd:float"/>
</xsd:simpleType>
which basically is the same as (except for the comma separator):
{
"type": "array",
"items": {
"type": "number"
}
}
then my answer is really about a "profile" that will be used by a conversion tool to take the semantics of a model represented in XSD (in other words, we're not hung up on XML documents here), into the same represented using JSON schema.
Our XSD-to-JSON schema profile uses a complex type to represent objects and arrays. When the "stereotype" applied to a complex type is "array", then the following model perfectly matches your JSON schema.
<xsd:complexType name="address">
<xsd:annotation>
<xsd:appinfo>
<xsd2json:type>array</xsd2json:type>
</xsd:appinfo>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="StreetNumber" type="xsd:float"/>
<xsd:element name="StreetName" type="xsd:string"/>
<xsd:element name="StreetType">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Street"/>
<xsd:enumeration value="Avenue"/>
<xsd:enumeration value="Boulevard"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="StreetDirection">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="NW"/>
<xsd:enumeration value="NE"/>
<xsd:enumeration value="SW"/>
<xsd:enumeration value="SE"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
Upvotes: 2