Skip to main content
Skip table of contents

Convert from XML

Input

✳️ Any (JSON)

Ouptut

✳️ Any (JSON) (Passthrough)

(plus) Response Data (object) (Optional)

Summary

This action takes a string containing an XML document (e.g. from a previous API call or other input), parses it, and converts it to JSON using the rules below.

Root Element Handling

By default, the root element is omitted, so:

XML
<Results>
  <Name>John Doe</Name>
  <Email>jdoe@example.org</Email>
</Results>

Becomes:

JSON
{
  "Name": "John Doe",
  "Email": "jdoe@example.org"
}

When the option Include Root Element is enabled, the output instead is:

JSON
{
  "Results": {
    "Name": "John Doe",
    "Email": "jdoe@example.org"
  }
}

Output Merging

By default, the Output Property is blank. This causes the contents to be merged into the existing data context.

Take, for example, this sample existing data context, with some trigger metadata and IQA results:

JSON
{
  "_metadata": {
    "id": "01K63E05595ZXD3Y5W8B9SZFFN"
  },
  "iqaResults": [...]
}

When the Output Property is blank, and Include Root Element is off, the resulting data context is:

JSON
{
  "_metadata": {
    "id": "01K63E05595ZXD3Y5W8B9SZFFN"
  },
  "iqaResults": [...],
  "Name": "John Doe",
  "Email": "jdoe@example.org"
}

When the Output Property is specified (for example, xmlData) and Include Root Element is off:

JSON
{
  "_metadata": {
    "id": "01K63E05595ZXD3Y5W8B9SZFFN"
  },
  "iqaResults": [...],
  "xmlData": {
    "Name": "John Doe",
    "Email": "jdoe@example.org"
  }
}

Finally, be careful when both an Output Property is specified, and Include Root Element is enabled, because your resulting context will be this, which includes two “root elements” of sorts:

JSON
{
  "_metadata": {
    "id": "01K63E05595ZXD3Y5W8B9SZFFN"
  },
  "iqaResults": [...],
  "xmlData": {
    "Results": {
      "Name": "John Doe",
      "Email": "jdoe@example.org"
    }
  }
}

XML to JSON Conversion Rules

All examples below assume that the “Include Root Element” setting is off.

Nested Elements

Nested elements are typically represented as nested JSON objects.

XML
<Result>
  <Item>
    <Name>John Doe</Name>
    <Email>jdoe@example.org</Email>
  </Item>
</Result>
JSON
{
  "Item": {
    "Name": "John Doe",
    "Email": "jdoe@example.org"
  }
}

Arrays

Anywhere that two or more elements exist at the same scope with the same name will create an array.

XML
<Result>
  <Item>
    <Name>John Doe</Name>
    <Email>jdoe@example.org</Email>
  </Item>
  <Item>
    <Name>Jane Doe</Name>
    <Email>jane@example.org</Email>
  </Item>
</Result>
JSON
{
  "Item": [
    {
      "Name": "John Doe",
      "Email": "jdoe@example.org"
    },
    {
      "Name": "Jane Doe",
      "Email": "jane@example.org"
    }
  ]
}

Attributes

Attributes appear as properties under the element, prefixed with the @ character:

XML
<Result>
  <Element1 Name="Joe" Title="Admin" />
  <Element2 Name="Jane" Title="Owner">
    <Course>9000-23</Course>
  </Element2>
</Result>
JSON
{
  "Element1": {
    "@Name": "Joe",
    "@Title": "Admin"
  },
  "Element2": {
    "@Name": "Jane",
    "@Title": "Owner",
    "Course": "9000-23"
  }
}

Mixed Text Content

Mixed text content will appear as a property under the parent element with the #text property name, for example:

XML
<Result>
  <Key Name="A1" />
  <Key Name="A2">
    <Value>true</Value>
    This key is an example.
  </Key>
</Result>
JSON
{
  "Key": [
    {
      "@Name": "A1"
    },
    {
      "@Name": "A2",
      "Value": "true",
      "#text": " This key is an example. "
    }
  ]
}

XML is a whitespace-sensitive language. Whitespace before and after an element (including newlines and indentation) is converted to at most 1 space character.

Be sure to separately trim any properties that may have whitespace around them prior to usage. This action does not support trimming of XML properties or text values.

CDATA Strings

CDATA tags (tags wrapped in <![CDATA[ … ]]>) are respected and parsed correctly as text nodes. They are available as a property named #cdata-section.

XML
<Result>
  <Key Name="A1" />
  <Key Name="A2" />
  <Key Name="A3" />
  <Key><![CDATA[
      <message>
        This is a CDATA string.
        It includes newlines and whitespace.
      </message>
   ]]></Key>
</Result>
JSON
{
  "Key": [
    {
      "@Name": "A1"
    },
    {
      "#cdata-section": " <message> This is a CDATA string. It includes newlines and whitespace. </message> "
    }
  ]
}

Properties

Name

Type

Templatable

Notes

XML Input

Text

(tick)

The XML document to parse.

The XML processing instruction preamble,
<?xml version="1.0" encoding="UTF-8"?>
is optional and ignored during parsing.

Include Root Element

Checkbox

(error)

If checked, the root element of the XML document is included, and all other elements are accessible under that root object.

This is in addition to the output property name.

If no root element is present on the XML document (i.e. the root of the document contains 2 or more elements as siblings), then a root element is automatically created and applied, named root.

🅾️ Output Property

Text

(error)

Optional. The property name to store the converted XML document data under.

If empty but Include Root Element is on, then the document’s root element will be added to the data pipeline.

If empty and Include Root Element is off, then the full contents of the XML document are merged with the existing data pipeline contents.

JavaScript errors detected

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

If this problem persists, please contact our support.