Moogsoft Docs

Utilities

The Utilities module is a JavaScript utility that allows you to escape XML so that Moogsoft AIOps correctly interprets control characters as data, not markup.

You can also use the module to convert an XML string to a JSON object, which is easier to manipulate in JavaScript. You can convert a JSON object to XML for external communication that requires XML input.

Load the Utilities Module

You can load the Utilities module into any standard Moobot or LAMbot.

To use, define a global object utilities at the top of a Moobot or LAMbot js file to load the Utilities module:

Moobots

var utilities = MooBot.loadModule('Utilities');

LAMbots

var utilities = LamBot.loadModule('Utilities');
Command Reference
utilities.escapeXML()

Escapes an XML string. Certain characters will not parse correctly if they are not escaped:

Unescaped character

Escaped string

"

"

'

'

<

&lt;

>

&gt;

&

&amp;

Request Argument

Name

Type

Description

value

String

The string to escape.

Example

var unescapedXML = 'my content requires "< and > "';
var escapedXML = '<tag>' + utilities.escapeXML(unescapedXML) + '</tag>';

The variable escapedXML now contains:

<tag>my content requires &quot;&lt; and &gt; &quot;</tag>
utilities.unescapeXML()

Unescapes an XML string.

Name

Type

Description

value

String

The string to unescape.

Example

var escapedXML = '<tag>my content requires &quot;&lt; and &gt; &quot;</tag>';
var unescapedXML = utilities.unescapeXML(escapedXML);

The variable unescapedXML now contains:

<tag>my content requires "< and > "</tag>
utilities.xmlToJSON()

Converts an XML string to a JSON object.

Name

Type

Description

value

XML string

The XML to convert to JSON.

Example

var xmlExample = '<alerts>' +
        '<alert enriched="false">' +
        '<id>1</id>' +
        '<description>Alert 1</description>' +
        '<host>email.moogsoft.com</host>' +
        '<severity>5</severity>' +
        '</alert>' +
        '<alert enriched="true">' +
        '<id>2</id>' +
        '<description>Alert 2</description>' +
        '<host>calendar.moogsoft.com</host>' +
        '<severity>2</severity>' +
        '</alert>' +
        '</alerts>';

var alerts = utilities.xmlToJSON(xmlExample);

The variablealertsnow contains:

{
  "alerts":{
    "alert":
    [
      {
        "severity":5,
        "host":"email.moogsoft.com",
        "description":"Alert 1",
        "id":1,
        "enriched":false
      },
      {
        "severity":2,
        "host":"calendar.moogsoft.com",
        "description":"Alert 2",
        "id":2,
        "enriched":true
      }
    ]
  }
}
utilities.jsonToXML

Converts a JSON object to an XML string. You can only use the utility to convert JSON objects, not arrays.

Name

Type

Description

value

JSON object

The JSON object to convert to XML.

Example

var jsonObjectExample =
{
  "data": {
    "alerts":
    [
      {
         "enriched": "false",
         "id": "1",
         "description": "Alert 1",
         "host": "email.moogsoft.com",
         "severity": "5"
      },
      {
         "enriched": "true",
         "id": "2",
         "description": "Alert 2",
         "host": "calendar.moogsoft.com",
         "severity": "2"
      }
    ]
  }
};

var convertedXML = utilities.jsonToXML(jsonObjectExample);

The variableconvertedXMLnow contains:

<data>
   <alerts>
      <severity>5</severity>
      <enriched>false</enriched>
      <host>email.moogsoft.com</host>
      <description>Alert 1</description>
      <id>1</id>
   </alerts>
   <alerts>
      <severity>2</severity>
      <enriched>true</enriched>
      <host>calendar.moogsoft.com</host>
      <description>Alert 2</description>
      <id>2</id>
   </alerts>
</data>