0

I am currently forwarding cloudwatch alarms to slack using a nodejs lambda function I found on github, however, when it receives a custom alert from one of our customers datacenters, the string is not in a JSON format, so it will not print the alert.

I am looking at rewriting the handleCatchAll() function to handle the string and just forward it to slack in its current format, but am having issues identifying the object.

Below is the code i am trying to edit, I need to write an if statement that will identify the object, say "if JSON.stringify fails, then render the message without JSON.stringify and input its value into 'description'"

var description = ""

if ()
{
  else if () 
  {
    for(key in message) {

        var renderedMessage = typeof message[key] === 'object'
                            ? JSON.stringify(message[key])
                            : message[key]

        description = description + "\n" + key + ": " + renderedMessage
    }
  }
}

1 Answers1

1

You can use this script to convert text input into json if the text is a valid JSON object, or else keep the original text.

const isJSON = str => {
    try {
        return (JSON.parse(str) && !!str);
    } catch (e) {
        return false;
    }
}
const regulizeRequestBody = body => isJSON(body) ? JSON.parse(body) : body;
liadperetz
  • 87
  • 3
  • 11