Skip to main content

Utilities

The Utilities module is a JavaScript utility that allows you to escape XML so that Moogsoft Enterprise 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 module

The Utilities module is available to load into any standard Moobot or LAMbot. To load it, define a new global variable at the top of the Moobot or LAMbot Javascript file. For example:

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

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

Method reference

The Utilities module uses the following methods.

escapeXML

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

Unescaped character

Escaped string

"

"

'

'

<

&lt;

>

&gt;

&

&amp;

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

String

Yes

The string to escape.

Example

Example use of the escapeXML method:

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>

unescapeXML

Unescapes an XML string.

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

String

Yes

The string to unescape.

Example

Example use of the unescapeXML method:

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>

xmlToJSON

Converts an XML string to a JSON object.

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

XML string

Yes

The XML to convert to JSON.

Example

Example use of the xmlToJSON method:

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 variable alerts now 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
      }
    ]
  }
}

jsonToXML

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

Request arguments

The method takes the following arguments:

Name

Type

Required

Description

value

JSON object

Yes

The JSON object to convert to XML.

Example

Example use of the jsonToXML method:

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 variable convertedXML now 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>