How to use XML with an API that only accepts a JSON request/response

Converting XML to and from a JSON payload

Many RESTful APIs only support a JSON payload in their request and response. But what if the source of the data you want to post to the API is in XML? Likewise, you may want to write the response from the API back to XML. In both cases, you'll need to transform XML to JSON and vice versa.

This demo use case simulates a scenario where data for product SKUs resides in an XML file. The data needs to be posted to an inventory system that has a RESTful API that only accepts a JSON request/response.

In this demo, the source XML will be read, converted to JSON, posted to the API, the response from the API will then be converted from JSON to XML and then written to disk.

How to download and run this data file conversion demo

  • If you haven't already done so, get yourself a Martini.
  • Within either Martini Desktop or Martini Online select the "Data File to Data File Transformations" demo from the Welcome Screen and click Install:
    screenshot-welcome-screen
  • The Packagedemo008-xml-file-to-rest will be automatically downloaded. Right-click the Package and click Start. A green icon will indicate it has started:
    screenshot-package
  • Documentation containing a Test Procedure to run the demo is included in the Package readme file \resources\readme\readme.md. The readme file is automatically opened when the Package is started.
    screenshot-documentation
  • The Test Procedure will guide you through the process of running a service to post data from an XML file to a RESTful API using a JSON payload and then taking the response and converting it back to XML.

Line by Line: How this "XML to API with JSON Payload" demo works

Pulling the XML files to be used for updating Inventory API SKU items

AddSkuStock is the service created to do this action. This service uses services generated by consuming the OpenAPI specification of Inventory API, as well as default Martini functions.

      • Line 3: Declare item \ This line is a Map Step with only one property declared. A Gloop Model called item is declared in this line that will hold the SKU data that will be used throughout the service.
      • Line 4: Fork Step \ In this line, the inputs of the service are validated in order to determine which input to use for reading the XML file.
        • If the InputStream has bytes to read, it means that the inputStream input property is used as input to this service.
        • If the InputStream has no bytes to read, it will proceed checking if the filename input property of this service is not null.
          • If there is a filename provided, it will execute the function in line 7. This function will get the InputStream of the XML to be read and sent to the Inventory API.
          • If filename is not provided when this service is executed manually, it will terminate the service execution, and no data will be saved in the SQL database.
      • Line 8: Xml.getInputCursorFromInputStream \ In this line, The InputStream is converted to an XML cursor in the form of a Model that you can use to map the data to Inventory API.
      • Line 9: Iterate over skuXml \ This line iterates all the SKU entries from the XML cursor generated from the previous step. If there are items to iterate:
        • Line 10: Map \ It will map the SKU record on the current iteration index to the item model.
        • Line 11: demo008.services_imported.stockIn.DemoApiInventoryServicesStockinInsertStockIn \ This is a service that was generated from consuming the Inventory API OpenAPI schema. The item model that was mapped in the previous step will be mapped to the body input of this service, which will be then sent to the Inventory API to add stocks to SKU.
        • Line 12: demo008.services_imported.sKU.DemoApiInventoryServicesSkuSelectSKU \ This is a service that was generated from consuming the Inventory OpenAPI schema. The sku_id of the SKU item in the current iteration index will be queried to the Inventory API by using this service to get the sku_name that will be used as the file name of the API response that will be converted to an XML file.
        • Line 13: File.packageOutputStream \ This is a default Martini function for creating an OutputStream we can use to write the API response to the XML file
        • Line 14: Fork \ Depending on what the response code is, it will write the corresponding API response to the OutputStream created in the previous step, before finally writing it into the file.