Convert javascript object to json object

I am creating a logMessage object.

logMessage = function (msg, severity, vendorId, userName, actionPerformed, priority, traceId, dataSent) {
    this.message = msg;
    this.severity = severity;
    this.vendorId = vendorId;
    this.userName = userName;
    this.actionPerformed = actionPerformed;
    this.priority = priority;
    this.traceId = traceId;
    this.dataSent = dataSent;
};

var msg = new logMessage(err, "High", "none", qry.username, "Error on login call: /req/login", "high", "", qry);
Utility.writeToLoggly(msg);

err ,qryare json objects; How to convert msg object to json object? I am sending an object msgin logglyto manage the log. It would be great if I could send a properly formed json object to loggly.

+4
source share
2 answers
logMessage = function (msg, severity, vendorId, userName, actionPerformed, priority, traceId, dataSent) {
    this.message = msg;
    this.severity = severity;
    this.vendorId = vendorId;
    this.userName = userName;
    this.actionPerformed = actionPerformed;
    this.priority = priority;
    this.traceId = traceId;
    this.dataSent = dataSent;
};

var msg = new logMessage(err, "High", "none", qry.username, "Error on login call: /req/login", "high", "", qry);
Utility.writeToLoggly(JSON.stringify(msg));
+3
source

you can use the JSON.stringify () function, so just add to your code:

Utility.writeToLoggly(JSON.stringify(msg));
+1
source

All Articles