How do I convert JSON to messageCache?

0 votes
asked Feb 17, 2016 by mike-r-7535 (13,650 points)
retagged Feb 17, 2016 by mike-r-7535
I want to reference attributes from a JSON message.  Is there an easy way to get those attributes into the messageCache?

1 Answer

0 votes
Yes.  The following script will create a messageCache value for each top level attribute of a JavaScript object.
var jsMessage = JSON.parse('' + message.getNode('/'));
for (var key in jsMessage) {
   if (jsMessage.hasOwnProperty(key)) {
      messageCache.setValue(key, jsMessage[key]);
Here are some notes on the script:
 - The JSON.parse function hydrates the JSON (JavaScript Object Notation) into a JavaScript object.
 - JavaScript objects are associative arrays, essentially a map of keys and values.
 - Message cache is a Java HashMap of string keys and string values.
 - The keyword "in" will return an array of keys that are defined on the JavaScript object.
 - hasOwnProperty() is a native JavaScript function to help us determine if this key was a user defined key, or if it is a native attribute for all JavaScript objects.  (ie: jsMessage.hasOwnProperty("hasOwnProperty") would return false because hasOwnProperty is a native attribute.)
 - jsMessage[key] is the syntax to access the value for the key.


answered Feb 17, 2016 by mike-r-7535 (13,650 points)