Page tree
Skip to end of metadata
Go to start of metadata

While XML sometimes seems to be something like a relic of the past in REST communication, there are still quite a few REST resources that respond to requests only with XML. So we have had a few PocketQuery customers asking us to implement XML support for PocketQuery REST datasources.

In PocketQuery 3.12.0, we now provide a solution: a built-in XML-to-JSON conversion for responses from REST datasources. 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:

<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:

{
  "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. Note that I also checked the "Debug" macro parameter to get some more information:

In the screenshot, we can first see a ClassCastException and the message "String cannot be cast to List". (Note that while I'm writing this I notice we should really work on some more user-friendly error-reporting. But that's another topic.) This error occurs because the response is not in the format expected by PocketQuery. 

You can also 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. So what now?

If you have used PocketQuery for a while, you'll know that we have a solution for such "unexpected format" problems in PocketQuery: Converters! For the above example, we could create a converter like this:

function convert(json) {
	var resultObject = JSON.parse(json);
	var index;
	var projects = [];
	var projectsInResult = resultObject.Projects.Project;
	for (var index in projectsInResult) {
		projects.push(projectsInResult[index]);
	}
	return projects;
}

The key point here is: 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:

{
  "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, I think 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. That will give you a hint on how to write a converter for it. If you need help, please don't hesitate to create a support request.

The result with the above converter will be this:

We wish you a happy XML'ing with PocketQuery!





  • No labels