Skip to content

message.setJSONObject

Signature: message.setJSONObject(nodePath, value*, instance*, forceNode*)

Returns: void

Find the node instance that matches nodePath (or create the node if it doesn't exist and forceNode = true) and set its value.

Note

The value is of type 'Object' and not 'String' making this call type-aware. JSON can be represented by JavaScript objects, arrays and native maps and Java maps, collections, raw arrays or anything that implements the org.mozilla.javascript.Scriptable interface.

Note

Calling message.setJSONObject(nodePath, value) without the instance parameter sets the first node instance that matches nodePath.

Note

This method only applies to JSON-formatted messages.

Parameters

Type Name Description Default
String nodePath the node path (XPath, HPath, Column ID, etc.)
Object value* (optional) the new node value null
Integer instance* (optional) the match instance to set. Use 1 for the first instance. 1
Boolean forceNode* (optional) create the node if it doesn't exist true

Example

// Example: Set JSON nodes to Object values, including replacing an object and creating a missing node.

// Sample JSON message structure
message = qie.createJSONMessage(JSON.stringify(
{
    userProfile: {
        name: "Jane Doe",
        age: 30,
        preferences: {
            theme: "light",
            notificationsEnabled: true
        }
    },
    accountStatus: "active"
}));

// Replace an existing object node
var newPreferences = {
    theme: "dark",
    notificationsEnabled: false,
    alertsEnabled: true
};
message.setJSONObject("/userProfile/preferences", newPreferences);
var preferences = message.getNode("/userProfile/preferences"); // updated object

// Create missing node and set it to an object
message.setJSONObject(
    "/userProfile/preferences/alertsConfig", // node path
    { enabled: true },                       // value
    1,                                       // instance
    true                                     // create node if it doesn't exist
);
var alertsConfig = message.getNode("/userProfile/preferences/alertsConfig"); // { "enabled": true }

// call setJSONObject with NativeMap using JSON.stringify()
var myMap = new Map();
myMap.set("enabled", true);
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    myMap
);

// call setJSONObject with NativeMap passed directly to setJSONObject()
var nativeMap = new Map();
nativeMap.set("enabled", true);
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    nativeMap
);

// call setJSONObject with NativeArray
var nativeArray = ["enabled", "true"];
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    nativeArray
);

// call setJSONObject with Java Map
var javaMap = new java.util.LinkedHashMap();
javaMap.put("enabled", true);
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    javaMap
);

// call setJSONObject with Java Collection
var javaList = new java.util.ArrayList();
javaList.add("enabled");
javaList.add("true");
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    javaList
);

// call setJSONObject with Java array
var javaArray = java.lang.reflect.Array.newInstance(java.lang.String, 3);
javaArray[0] = 'enabled';
javaArray[1] = 'true';
message.setJSONObject(
    "/userProfile/preferences/alertsConfig",
    javaArray
);