- 23 May 2023
-
DarkLight
-
PDF
setObjectKey
- Updated on 23 May 2023
-
DarkLight
-
PDF
setObjectKey Task Purpose
The setObjectKey task allows you to set a property with a value. You can access the property through its path
, which is an array. The path
identifies the array to change/process (modify).
This task modifies an object. The basic idea is that a value can be set by passing the object, and then a path, followed by the value that will be used to set the property. In the event that the property is not there (undefined), then a path to it will be created.
Potential Use Cases
With this task, you can change an item of data in a key value by specifying the path to the key. Also if a key is non-existent then this task can create the specified key in the object.
Let's say you have a data object like this:
let data = { user: { name: "John Doe", age: 34, occupation: "analyst"} };
Using setObjectKey, you can set a new key value for user
such as experience
("senior"
, "junior"
, "entry"
), or another demographic value such as location
("USA"
, "Canada"
, "Australia"
).
Let's consider another example. Sometimes you may get a complicated object back from an API endpoint, in which the object has nested levels of data and depending on what parameter you send to the API, your query may fail and return an error because the object may or may not have the parameter that was requested.
You could use setObjectKey
if you want to set deeply nested properties on an object in an environment where you're not sure if all the properties in the path already exist and you don't want to deal with the logic of checking for and setting each key manually in more than one place. The task would allow you to quickly set the needed value without checking if the keys were present previously or not.
Properties
Input and output properties are shown below.
Input | Type | Description |
---|---|---|
obj |
Object | Required. The object to modify. |
path |
Array | Required. The path of the property to set. |
value |
* | The value to set into path . Wildcard values (* ) represent all values that can be inserted/overwritten into a given path . |
Output | Type | Description |
---|---|---|
object |
Object | The modified object that is returned. |
Example 1
In the IAP examples shown below:
-
All the incoming property variables (
obj
,path
,value
) are required. The selected reference task (used to define the data source) for each variable isjob
. -
The job variables for
setObjectKey
are manually set prior to running the workflow. -
After running the workflow, the Outgoing tab in Task History, which is accessed from Jobs (Operations Manager), displays the result of executing the task. Notice the object was modified (changed) and the
path
now includes"title": "developer"
as a set property value.
Example 2
In this IAP example:
-
Notice the job variables that were set prior to running the workflow.
-
After running the workflow, the modified object is returned with
"Adam":"developer"
as a set property in the givenpath
.