message.setJSONArray¶
Signature: message.setJSONArray(nodePath, 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 it to an empty array.
Note
Calling message.setJSONArray(nodePath) 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.) | |
| 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¶
// Given a JSON message like:
message = qie.createJSONMessage(
JSON.stringify(
{
"people": [
{
"name": "Thomas",
"quarterly_scores": [100, 97, 88]
},
{
"name": "Sally",
"quarterly_scores": [100, 100, 99]
}
],
"animals": ["cat", "dog", "horse"]
}
)
);
// Set the first matching array node to an empty array
message.setJSONArray("/animals");
// Set the second instance of quarterly_scores to an empty array
message.setJSONArray("/people/quarterly_scores", 2); // instance (1 = first instance)
// Create and set a missing array node using forceNode = true
message.setJSONArray("/people/[]/children", 1, true);
// Resulting JSON:
// {
// "people": [
// {
// "name": "Thomas",
// "quarterly_scores": [100, 97, 88],
// "children": []
// },
// {
// "name": "Sally",
// "quarterly_scores": []
// }
// ],
// "animals": []
// }