I would like to get the XSD schema for the below JSON Body.
[
{"text": "Hello"},
{"text": "How are you?"}
]
For instance, if I convert online the above JSON body to XML and then to XSD schema, it gives the below output.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="row">
<xs:complexType>
<xs:sequence>
<xs:element name="text" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The intent is to use the XSD schema in Informatica Rest web consumer transformation.
With the above XSD schema, the JSON body is creating as below, which is not desired request body:
{"row": [
{"text": "Hello"},
{"text": "How are you?"}
]}
Is there anyway, I can skip the row tag?
Any help in this regard?
JSON and XML have different data models, which means that lossless round-tripping is generally impossible. If you want to achieve it, you will need to write custom code that understands your specific data. A good tool for this job is XSLT 3.0.
I have defined a xml scehema below
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
<xs:element name="Root">
<xs:complexType>
<xs:all>
<xs:element name="bblist">
<xs:complexType>
<xs:sequence>
<xs:element name="item" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
I want to generate the Json below using a pipeline.
{
"bblist":
[
"13403"
]
}
but the BizTalk pipeline converts it to
{"bblist": "13403"}
Just wondering if my schema is correct. Am I defining the xsd to generate Json arrays correctly?
There are three issues with your XSD schema
You haven't defined a target Namespace. Which means when it goes through the XML Receive it sets the MessageType to a default set of values which doesn't reference the schema. Which means it might not know which schema to use in the JSON Encoder.
MessageType Root Promoted http://schemas.microsoft.com/BizTalk/2003/system-properties
You have used a <xs:all> rather than a <xs:sequence> in your schema definition. That the JSON Encoder doesn't handle.
If you define your schema as this
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="bblist">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="item" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
With a payload of
<ns0:Root xmlns:ns0="http://bblist">
<bblist>
<item>item_0</item>
</bblist>
</ns0:Root>
You get a output of
{
"bblist": {
"item": [
"item_0"
]
}
}
This is closer to your expected JSON, with it making an array of the repeating element.
Your structure is incorrect for the JSON you are expecting as you have the repeat on the item and not on the blist.
If you define your schema as
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://bblist" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://bblist" vc:minVersion="1.1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" name="blist" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XML is
<ns0:Root xmlns:ns0="http://bblist">
<blist>blist_0</blist>
</ns0:Root>
JSON is
{
"blist": [
"blist_0"
]
}
I generate a WADL with CXF as described in CXF – Missing WADL method parameter element types with JSON JAX-RS services.
I tried to generate the JSON request body in SoapUI ("Recreates a default representation from the schema"), but I get the wrong JSON including namespace.
Code:
CXF configuration:
<bean id="wadlGenerator" class="org.apache.cxf.jaxrs.model.wadl.WadlGenerator">
<property name="linkJsonToXmlSchema" value="true" />
</bean>
<jaxrs:server address="/rest/1" id="test" staticSubresourceResolution="true">
<jaxrs:serviceBeans>
<ref bean="testResource" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" />
<ref bean="wadlGenerator" />
</jaxrs:providers>
</jaxrs:server>
WADL:
<?xml version="1.0"?>
<application xmlns:prefix1="http://www.test.com/test" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://wadl.dev.java.net/2009/02">
<grammars>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:import/>
<xs:element name="testModel" type="testModel"/>
</xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/test" targetNamespace="http://www.test.com/test" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:complexType name="testModel">
<xs:sequence>
<xs:element name="id" type="xs:string"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</grammars>
<resources base="http://localhost:8080/test-app/services/rest/1">
<resource path="/test">
<method name="POST">
<request>
<representation mediaType="application/json" element="prefix1:testModel"/>
</request>
<response status="204"/>
</method>
</resource>
</resources>
</application>
JSON:
{
"#xmlns:test": "http://www.test.com/test",
"id": "?",
"name": "?"
}
Is that a bug/missing feature of SoapUI or is there something wrong with my WADL?
I am validating a Java object using the Validator API. THis works as far as telling me that the object is not valid according to the schema, but it gives a very vague error message that one of the elements from a list of all the element's properties are missing.
Schema (truncated for clarity):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://predictivesolutions.com/schema/v1_1" xmlns:snt="http://predictivesolutions.com/schema/v1_1"
elementFormDefault="qualified">
<xs:element name="contactInsert">
<xs:complexType>
<xs:all>
<xs:element name="suffix" type="snt:stringType255"
minOccurs="0" maxOccurs="1" />
<xs:element name="firstName" type="snt:stringType255"
minOccurs="1" maxOccurs="1" />
<xs:element name="lastName" type="snt:stringType255"
minOccurs="1" maxOccurs="1" />
<xs:element name="companyID" type="snt:idType" minOccurs="1"
maxOccurs="1" />
<xs:element name="companyLocationID" type="snt:idType"
minOccurs="1" maxOccurs="1" />
<xs:element name="workPhone" type="snt:phoneType"
minOccurs="0" maxOccurs="1" />
<xs:element name="cellPhone" type="snt:phoneType"
minOccurs="0" maxOccurs="1" />
<xs:element name="homePhone" type="snt:phoneType"
minOccurs="0" maxOccurs="1" />
<xs:element name="otherPhone" type="snt:phoneType"
minOccurs="0" maxOccurs="1" />
I am basically deserializing a JSON object to the JAXB generated class from the schema above. Again this works:
ValidationErrorHandler errorHandler = null;
try {
JAXBContext jc = JAXBContext.newInstance(ContactInsert.class);
JAXBSource source = new JAXBSource(jc, contactInsert);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(ContactInsert.class
.getResource("/xsd/v1_1/contact.xsd"));
Validator validator = schema.newValidator();
errorHandler = new ValidationErrorHandler();
validator.setErrorHandler(errorHandler);
validator.validate(source);
My JSON object gets converted to the ContactInsert instance:
{ "companyID":666, "lastName":"dsadasd", "companyLocationID":23950, "otherComments":null }
I would expect the validation error to be that "firstName" is expected, however it lists every element from the schema, even though these are not required (minOccurs=0 and not annotated with #Required in the generated Java class:
"cvc-complex-type.2.4.b: The content of element 'contactInsert' is not
complete. One of
'{"http://predictivesolutions.com/schema/v1_1":suffix,
"http://predictivesolutions.com/schema/v1_1":firstName,
"http://predictivesolutions.com/schema/v1_1":workPhone,
"http://predictivesolutions.com/schema/v1_1":cellPhone,
"http://predictivesolutions.com/schema/v1_1":homePhone,
"http://predictivesolutions.com/schema/v1_1":otherPhone,
"http://predictivesolutions.com/schema/v1_1":faxPhone,
"http://predictivesolutions.com/schema/v1_1":email,
"http://predictivesolutions.com/schema/v1_1":isDesignate,
"http://predictivesolutions.com/schema/v1_1":isActive,
"http://predictivesolutions.com/schema/v1_1":mainSponsorID,
"http://predictivesolutions.com/schema/v1_1":hidePercentSafeFlag,
"http://predictivesolutions.com/schema/v1_1":externalContactID,
"http://predictivesolutions.com/schema/v1_1":useAsARef,
"http://predictivesolutions.com/schema/v1_1":positionTitle,
"http://predictivesolutions.com/schema/v1_1":otherComments,
"http://predictivesolutions.com/schema/v1_1":formerCompanyID,
"http://predictivesolutions.com/schema/v1_1":industryStartDate,
"http://predictivesolutions.com/schema/v1_1":employmentStartDate}' is
expected."
Is it possible to configure the validator to only report the required field ("firstName"). I am relatively new to JAXB, but it seems like that error message is not accurate, and that JAXB is simply taking the easy way out by saying one of the elements is missing, but I will not tell you which.
Where is the Error Coming From
The error message being returned is from the javax.xml.validation.Validator and not JAXB. In this use case JAXB is simply the XML source. If you swapped in a StAXSource the StAX parser wouldn't become responsible for the schema validation.
Is it a Good Error?
OK...but why does validator report all those fields in the error
message?
Here is the error you are getting:
"cvc-complex-type.2.4.b: The content of element 'contactInsert' is not
complete. One of
'{"http://predictivesolutions.com/schema/v1_1":suffix,
"http://predictivesolutions.com/schema/v1_1":firstName,
"http://predictivesolutions.com/schema/v1_1":workPhone,
"http://predictivesolutions.com/schema/v1_1":cellPhone,
"http://predictivesolutions.com/schema/v1_1":homePhone,
"http://predictivesolutions.com/schema/v1_1":otherPhone,
"http://predictivesolutions.com/schema/v1_1":faxPhone,
"http://predictivesolutions.com/schema/v1_1":email,
"http://predictivesolutions.com/schema/v1_1":isDesignate,
"http://predictivesolutions.com/schema/v1_1":isActive,
"http://predictivesolutions.com/schema/v1_1":mainSponsorID,
"http://predictivesolutions.com/schema/v1_1":hidePercentSafeFlag,
"http://predictivesolutions.com/schema/v1_1":externalContactID,
"http://predictivesolutions.com/schema/v1_1":useAsARef,
"http://predictivesolutions.com/schema/v1_1":positionTitle,
"http://predictivesolutions.com/schema/v1_1":otherComments,
"http://predictivesolutions.com/schema/v1_1":formerCompanyID,
"http://predictivesolutions.com/schema/v1_1":industryStartDate,
"http://predictivesolutions.com/schema/v1_1":employmentStartDate}' is
expected."
This implementation has chosen to list all of the possible elements that may occur here. If only the required elements where listed someone could have opened a question similar to this one as to why the optional elements were omitted.
Here is the schema I am using for JiBx to codegen and bind.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.abc.com/abc/service/APIService" targetNamespace="http://www.abc.com/abc/service/Service" elementFormDefault="qualified" attributeFormDefault="unqualified" version="2.0">
<xs:include schemaLocation="OTA_AirLowFareSearchRQ.xsd"/>
<xs:include schemaLocation="OTA_AirLowFareSearchRS.xsd"/>
<xs:element name="APIRequest">
<xs:complexType>
<xs:sequence>
<xs:element ref="OTA_AirLowFareSearchRQ"/>
</xs:sequence>
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="bdmVersion" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="APIResponse">
<xs:complexType>
<xs:sequence>
<xs:annotation>
<xs:documentation xml:lang="en">All Schema files in the OTA specification are made available according to the terms defined by the OTA License Agreement at http://www.opentravel.org/ota_downloads_form.cfm</xs:documentation>
</xs:annotation>
<xs:element ref="OTA_AirLowFareSearchRS"/>
</xs:sequence>
</xs:complexType>
</xs:element>
and this is the error I am getting when trying to generate code.
ERROR codegen.CodeGen - Error: Referenced element '{http://www.abc.com/abc/service/APIService}:OTA_AirLowFareSearchRQ' is not defined for 'element' at (line 11, col 47, in APIService.xsd).
Any help is highly appreciated.
Narayanan,
Your xml namespaces are incorrect. Your error message is telling you what is wrong. '{http://www.abc.com/abc/service/APIService}:OTA_AirLowFareSearchRQ' is not defined, because OTA_AirLowFareSearchRQ is in the {http://www.opentravel.org/OTA/2003/05} namespace.
The correction is simple, either include the element from the correct namespace, or more simply place your schema in the opentravel name space. Here is your corrected schema definition:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.opentravel.org/OTA/2003/05"
targetNamespace="http://www.abc.com/abc/service/Service"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
version="2.0">
<xs:include schemaLocation="http://www.opentravel.org/2011A/OTA_AirLowFareSearchRQ.xsd"/>
<xs:include schemaLocation="http://www.opentravel.org/2011A/OTA_AirLowFareSearchRS.xsd"/>
<xs:element name="APIRequest">
<xs:complexType>
<xs:sequence>
<xs:element ref="OTA_AirLowFareSearchRQ"/>
</xs:sequence>
<xs:attribute name="version" type="xs:string" use="required"/>
<xs:attribute name="bdmVersion" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
<xs:element name="APIResponse">
<xs:complexType>
<xs:sequence>
<xs:annotation>
<xs:documentation xml:lang="en">All Schema files in the OTA specification are made available according to the terms defined by the OTA License Agreement at http://www.opentravel.org/ota_downloads_form.cfm</xs:documentation>
</xs:annotation>
<xs:element ref="OTA_AirLowFareSearchRS"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I tested this with JiBX and it Should work fine now!
Don