Skip to main content
Skip table of contents

Handling XML-based REST responses

While XML sometimes seems to be something like a relic of the past in REST communication, there are still quite a few REST APIs that respond to requests only with XML. In PocketQuery, a built-in XML-to-JSON conversion for responses from REST datasources takes place. PocketQuery will detect if your response is in XML format and go ahead with this transformation.

Example Auto-Conversion

For example, assume this is your XML response:

CODE
<Projects>
  <Project>
    <ID>1234567</ID>
    <Name>My Project Name</Name>
    <Number>P123456</Number>
    <Description>This is an awesome description for this project.</Description>
    <CreatedDate>2019-01-15T15:00:00</CreatedDate>
    <ModifiedDate>2019-11-08T09:23:00</ModifiedDate>
    <CreatedByFirstName>Stephen</CreatedByFirstName>
  </Project>
</Projects>

Your auto-converted result will be:

CODE
{
  "Projects":{
    "Project":{
      "Description":"This is an awesome description for this project.",
      "Number":"P123456",
      "CreatedDate":"2019-01-15T15:00:00",
      "ID":1234567,
      "ModifiedDate":"2019-11-08T09:23:00",
      "CreatedByFirstName":"Stephen",
      "Name":"My Project Name"
    }
  }
}

Analysis & Processing the Result with a Converter

Without further work, PocketQuery will now display an error: "String cannot be cast to List". This error occurs because the response is not in the format expected by PocketQuery. 

If you enable the debug mode, you can see an info box titled "Raw result retrieved from the datasource" that shows the raw XML string and you can see a "Result passed to the converter" info box. Here you can see the input and the output of the XML auto-conversion: PocketQuery detected that the response is an XML string and converted it to a JSON string. A converter needs to be created.

In the above example, the actual projects array will be accessible in the result with the key resultObject.Projects.Project. This is because the XML result was converted to this structure:

CODE
{
  "Projects":{
    "Project":[
      {
        "Name":"Project 1",
        ...
      },
      {
        "Name":"Project 2",
        ...
      }
    ]
  }
}

We are using a Java library called JSON-Java for this conversion. A more detailed article about this library can be found here. Another example of how this conversion takes place can be found here. In general, it makes most sense to use the "Debug" parameter in the PocketQuery macro and then see what the raw XML result was and to what JSON it was converted.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.