简体   繁体   中英

How to convert JSON array to XML element for WADL

I am trying to retrieve data from a web service and the proprietary ETL tool (which I have to use) requires a .WADL file for the connection to the REST service.

So far, I have created a .WADL file from the API endpoint with the help of SOAP UI and after that I have put the schema definition (for the response) between the <grammars> </grammars> tags. For that, I have converted the expected JSON output from the web service to XML and then to XSD with the help of an online formatter.

This works fine for most of the original JSON elements however when the element is a JSON array (without names), my ETL tool is not able to parse the value.

To illustrate, the output from the web service looks similar to this (I shortened the output a bit):

{
   "report":    {
      "type": "overtime",
      "elements": [      {
         "id": "datetime",
         "name": "Date"
      }],
      "totals": ["51245123"]
   }
}

The XSD then looks like this (after I have already removed some unneeded elements with name "element" which the online converter put there as a replacement for the original JSON arrays (eg "elements")):

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="report">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="elements">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="id"/>
                    <xs:element type="xs:string" name="name"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="totals">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:int" name="element"/>
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element type="xs:string" name="type"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

When I use the above XSD in my .WADL file, then my tool fails while parsing the "totals" element because of a mismatch. When I remove the complex type element named "element" and just use <xs:element type="xs:int" name="totals"/> , then I get values for all properties except for the element name "totals". This appears as null.

That means I can get data for the JSON array "elements" but not for "totals". As I see it, my problem is only with 'normal' JSON arrays without a name because I cannot bind them to an XS element with the same name.

If I were to write code I would try to get the value from totals[0] but I have no control over how the tool is parsing the JSON response from the server. However, since I am able to get the values for all the properties (also from other JSON arrays which are using names/keys) I guess there is a way to also make it work for 'normal' JSON arrays.

Can anyone advise me on how to deal with those JSON arrays in XSD?

Thank you.

Since I'm feeling lucky today, I would still suggest to try this out for the totals element:

<xs:element name="totals" type="xs:string"/>

or if the tool can handle the cast:

<xs:element name="totals" type="xs:int"/>

(Looking at the way the elements array is mapped to XML, it seems that a simple type would be more appropriate than a complex type for totals ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM