Sidebar

What does it mean when I see [object Object] while trying to debug JSON?

+1 vote
257 views
asked Mar 9, 2016 by rich-c-2789 (15,250 points)

I have a sample message like:

<Contact>
   <FirstName>Rich</FirstName>
   ...more elements etc.
</Contact>
with code like this:
var contact = {};
if (message.checkNodeExists('/Contact/FirstName')){
  contact.firstName = message.getNode('/Contact/FirstName');
}
qie.debug("contact: " + contact);
the debug return this result: 
[path=1-2] - contact: [object Object]
What does [object Object] mean and how can I fix it?

1 Answer

+1 vote
This is due to the JSON object being converted to a string but the object does not overwrite the parent objects toString method.  Here is an example that implements a toString function on the contact:  
var contact = {};
contact.toString = function() { return this.firstName;};
if (message.checkNodeExists('/Contact/FirstName')){
   contact.firstName = '' + message.getNode('/Contact/FirstName');
}
qie.debug("contact: " + contact);
This returns:
contact: Rich
However, that is probably not what you really wanted to do just to debug it.  In this case just use JSON.stringify() like so:
var contact = {};
if (message.checkNodeExists('/Contact/FirstName')){
   contact.firstName = '' + message.getNode('/Contact/FirstName');
}
qie.debug("Stringified contact: " + JSON.stringify(contact));
The results:
Stringified contact: {"firstName":"Rich"}
Additionally, you might see [object null], [object, Function] etc.  In which case check out this link for more detail: http://stackoverflow.com/questions/4750225/what-does-object-object-mean
 
Also, in case you were wondering why the code above has:
'' +
after the equal sign, see this question: 
answered Mar 9, 2016 by rich-c-2789 (15,250 points)
...