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.