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.
Related
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 am using BizTalk pipeline with JSON encoder to convert XML to JSON .
I have created the XSD but the JSON generated has #text instead of just a value for my elements.
Any ideas what I'm doing wrong?
<xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://BookingEngine.Schemas.JSONSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="affiliate_reference_id" type="xs:unsignedShort" />
<xs:element minOccurs="1" name="hold" type="xs:boolean" />
<xs:element minOccurs="1" maxOccurs="unbounded" name="rooms">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="email" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="1" maxOccurs="unbounded" name="payments">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" name="type" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element minOccurs="1" name="affiliate_metadata" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
is converted to the following JSON
{
"affiliate_reference_id": {
"#text": "4480"
},
"hold": {
"#text": "false"
},
"rooms": [
{
"email": "john#example.com"
}
],
"payments": [
{
"type": "customer_card"
}
]
}
expected results would be
{
"affiliate_reference_id": "4480",
"hold": "false",
"rooms": [
{
"email": "john#example.com"
}
],
"payments": [
{
"type": "customer_card"
}
]
}
Any idea why the #text is popping up and how to remove it?
What do I need to change in my XSD schema?
I've the same result in c# when using Newtonsoft. The #text nodes are added when they have a different namespace than the root-namespace because in JSON it becomes an object with a text and a attribute namespace.
I added a custom biztalk pipeline and used json newton soft nuget package .
I created a pipeline with a JSON encoder and had this same issue.
The secret is you need to add an XML encoder before the JSON encoder in your pipeline. Yes, it's odd.
Create a schema with the correct data types for the JSON results you want. Add that schema to the XML encoder document schema property. The schema allows you to control how the JSON encoder works.
Note: XML requires a single root node but JSON does not. In your JSON assembly schema create a root node with any name you like. In the JSON encoder there's a check box to remove the root node enclosing the payload.
Javascript data types are not the same as XML (such as dates) so some translation may be needed.
I have the below mentioned source schema.
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://schemas.microsoft.com/Sql/2008/05/TypedPolling/EmailNotifications" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TypedPolling">
<xs:complexType>
<xs:sequence>
<xs:element name="TypedPollingResultSet0">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="TypedPollingResultSet0">
<xs:complexType>
<xs:sequence>
<xs:element name="strPortName" type="xs:string" />
<xs:element name="LastRun_UTC" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The destination schema is
<?xml version="1.0" encoding="utf-16" ?>
<xs:schema xmlns="http://_024_EmailNotifications_Schemas.BizTalkDTADBExtractMod" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://_024_EmailNotifications_Schemas.BizTalkDTADBExtractMod" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Notification">
<xs:complexType>
<xs:sequence>
<xs:element name="strPortName" type="xs:string" />
<xs:element name="LastRun_UTC" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I have a map as shown below
The input file for the map is
<ns0:Root xmlns:ns0="http://schemas.microsoft.com/BizTalk/2003/aggschema">
<InputMessagePart_0>
<ns1:EmailNotifications xmlns:ns1="http://024_EmailNotifications_Schemas.XMLConfig">
<App>
<Name>App1</Name>
<Port>Port1</Port>
<Email>email</Email>
</App>
<App>
<Name>App2</Name>
<Port>Port2</Port>
<Email>Email2</Email>
</App>
<App>
<Name>App3</Name>
<Port>Port3</Port>
<Email>Email3</Email>
</App>
<App>
<Name>App4</Name>
<Port />
<Email>Email4</Email>
</App>
</ns1:EmailNotifications>
</InputMessagePart_0>
<InputMessagePart_1>
<ns2:TypedPolling xmlns:ns2="http://schemas.microsoft.com/Sql/2008/05/TypedPolling/EmailNotifications">
<TypedPollingResultSet0>
<TypedPollingResultSet0>
<strPortName>Port1</strPortName>
<LastRun_UTC>2016-01-29T10:20:10.083Z</LastRun_UTC>
</TypedPollingResultSet0>
<TypedPollingResultSet0>
<strPortName>Port2</strPortName>
<LastRun_UTC>2016-01-29T11:37:38.82Z</LastRun_UTC>
</TypedPollingResultSet0>
<TypedPollingResultSet0>
<strPortName>Port3</strPortName>
<LastRun_UTC>2016-01-29T11:37:39.353Z</LastRun_UTC>
</TypedPollingResultSet0>
</TypedPollingResultSet0>
</ns2:TypedPolling>
</InputMessagePart_1>
</ns0:Root>
When I try to run the debug option in the map, I get this error which is preventing me from debugging the map, and I don't have a clue how to fix it. Appreciate any help I can get
E:\temp\EmailNotifications\InputInternal.xml: error btm1044: Input validation error: The element 'TypedPolling' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedPolling/EmailNotifications' has invalid child element 'TypedPollingResultSet0' in namespace 'http://schemas.microsoft.com/Sql/2008/05/TypedPolling/EmailNotifications'. List of possible elements expected: 'TypedPollingResultSet0'.
When you do not specify the maxOccurs, it defaults to 1. Try setting the nested <TypedPollingResultSet0> element's maxOccurs to unbounded:
<xs:element name="TypedPollingResultSet0">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="TypedPollingResultSet0">
I'm not 100% sure of what corrected my issue.
I changed the name space from ..../EmailNotifications to ..../EmailNotification
That resolved the wiggly line issue in the input xml file in the visual studio.
Then I regenerated the schema file from the input file and viola the issue got resolved.
Thanks Gruff for the help
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
I've got what seems like a very simple example of an xsd and xml file where the xml file does not validate.
Copies of the two files are below.
The first xml element with id = 'fixMe' gets an error that says
Top level is not completed.
Valid xml document must have a root tag
If I remove its id attribute or use "idx" instead of "id", it's fine. But I can't figure out why. 'id' should be a valid attribute.
Any insight appreciated.
XML:
<question id="fixMe" />
<question idx="ok"/>
<question />
XSD:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="perceptive.com/mi/analysis"
xmlns="perceptive.com/mi/analysis"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="test" type="test"/>
<xs:complexType name="test">
<xs:sequence minOccurs="1" maxOccurs="10">
<xs:element name="question" type="question_type"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="question_type">
<xs:attribute name="idx" type="xs:string" use="optional"/>
<xs:attribute name="id" type="xs:string" use="optional"/>
</xs:complexType>
</xs:schema>
An XML document cannot be valid until it is well-formed.
So you should specify a root tag in XML document, e.g.:
<root>
<question id="fixMe" />
<question idx="ok"/>
<question />
</root>