Table of Contents | ||||
---|---|---|---|---|
|
Object Literals
...
In this Page
Table of Contents | ||||||
---|---|---|---|---|---|---|
|
Object Literals
Description | Object literals allow you to construct an object with a set of properties. Object literals function similar to JavaScript object literals. Within the object literal, the following variables are available:
| ||||
---|---|---|---|---|---|
Syntax | An object literal is a comma-delimited list of zero or more pairs of property names and values surrounded by curly braces ({}), like so:
The property name can be computed dynamically by enclosing an expression in square brackets ([]), like so:
The result of the expression will be converted into a string. | ||||
Example |
|
Object Methods
The following methods are shared among all object types.
entries
Description | Returns an array of the given object's own enumerable property [key,value] pairs. See also keys, values. | ||
---|---|---|---|
Syntax |
| ||
Example | Input:
Expression: $user.entries() |
extend
Description | Returns a new object with the properties of the current one merged with the properties of the objects that were passed in. This is similar to http://underscorejs.org/#extend, but the extend object method returns a new object instead of modifying the given one.
The extend object method can also be used to convert an array of objects into an object. An example illustrating the same is described in the Example Use Cases section below. | ||||||
---|---|---|---|---|---|---|---|
Syntax |
| ||||||
Example | Expression: $.extend({ newField1 : 'foo' }, { newField2 : 'bar' }) Input:
Result:
|
get
Description | Get the value of a property or a default value if the object does not have the given property. If no default value is given, null is returned.
See also: Checking for optional properties and returning defaults in the expression language | ||
---|---|---|---|
Syntax |
| ||
Example | Expression: Result: Returns the value of the "Id" property or null if the object does not have the property. Expression: Result: Returns the value of the "Id" property or the number "123" if the object does not have the property. |
...
getFirst
Description | Indicates whether Find the object has value for the specified given property .
| Syntax | Code Block | name. If the property does not exist, the function returns the default value (if given); otherwise, null is returned. If the value is a populated list, then the function returns the first value in the list; otherwise, it returns the default value if present or null. | ||||
---|---|---|---|---|---|---|---|---|
Syntax |
| |||||||
Example | Expression: Result: Returns true if the object has the key "Id" Expression: To create a ternary conditional expression:
|
hasPath
Where $test is the string "abc123" Result: abc123 Expression: Where $test is a list consisting of [5, 10, 15, 20] Result: 5 |
hasOwnProperty
Description | Returns a boolean value to indicate whether the object has the specified property. |
---|
Paste code macro |
---|
object.hasPath(field) |
Expression: $.hasPath("Id")
Result: Returns true if the object has the key "Id"
isEmpty
Description |
---|
Note |
---|
This function is not supported in Spark pipelines. |
Code Block |
---|
object.isEmpty() |
Expression: {}.isEmpty()
The in operator and get method can be used as a shorthand to test if an object has a property or get the value of a property with a default if it does not exist.
Note |
---|
This argument is treated as a JSONPath instead of a plain property name and returns false if the property exists but the value is null. Most expressions should work as expected if the object is not a qualified path. If your expression passes a JSONPath or if the property value is null, use the hasPath method instead. For example, if we use this object method to look up a property in an object, then we would have the following expression:
Where the property "route" is the target property in the object $.map. Using this object method to find a path does not work as might be expected. For example:
Where map.route is a path and not a property. In this case, we recommend you use the hasPath() method. |
Code Block |
---|
object.hasOwnProperty(field) |
Expression: $.hasOwnProperty("route")
where $
object corresponds to my.custom.route
Result: Returns true.
Expression: { foo: 1 }.isEmpty() $.hasOwnProperty("custom.route")
where $
object corresponds to my.custom.route
Result:
- Returns false
filter
Description | Create a new object that retains some properties from the original as specified by the given callback. | |||
---|---|---|---|---|
Syntax |
Code Block |
---|
object.filter(callback) |
- callback - A function that takes three arguments (property-value, property-name, input-object) and returns true if the property should be included in the returned object or false if it should be left out.
- if the value of
route
is null ascustom.route
is not a property but a path. (Reason to use hasPath() function instead.) - Returns true if the value of
route
is not null thoughcustom.route
is not a property but a path.
Expression: To create a ternary conditional expression:
$.hasOwnProperty('query') ? $query : 'not present in input'
hasPath
Description | Returns a boolean value to indicate whether the object has the specified property (path) that carries a non-null value. This method is recommended when working with JSONPath, especially when looking for fields nested deep within the object.
| |||||||
---|---|---|---|---|---|---|---|---|
Syntax |
| |||||||
Example | Expression: Result: Returns true. Expression: Result:
Example JSON Object:
Expression: Result: Returns true because the object "Type" has a specified value. Expression: Result: Returns true because "EditorName" has a specified value. Expression: Result: Returns false since even though the object has the key "Owner", it is null. Expression: Result: Returns false since the object does not have "Region". |
isEmpty
Description | Returns true if the given object has no properties.
| ||
---|---|---|---|
Syntax |
| ||
Example | Expression: {}.isEmpty() Result: Returns true. Expression: { foo: 1 }.isEmpty() Result: Returns false. |
filter
Description | Create a new object that retains some properties from the original as specified by the given callback. | ||||
---|---|---|---|---|---|
Syntax |
| ||||
Example | Expression: $.filter((value, key) => key.startsWith("new")) Input:
Result:
|
keys
Description | Returns an array of strings that represent all the enumerable properties of the given object. The ordering of the properties is the same as that given by looping over the properties of the object manually. See also values, entries. | ||
---|---|---|---|
Syntax |
| ||
Example | Input:
Expression: $user.keys() |
mapKeys
Description | Transform the names of properties in an object using a callback. This is similar to https://lodash.com/docs/4.17.4#mapKeys | |||||
---|---|---|---|---|---|---|
Syntax |
| |||||
Example | Expression:Expression: Input:
Result:
|
mapValues
Description | Transform the values of properties in an object using a callback. This is similar to https://lodash.com/docs/4.17.4#mapValues | ||||
---|---|---|---|---|---|
Syntax |
| ||||
Example | Expression: Input:
Result:
|
merge
Description | Perform a deep merge of this object with those passed in. The method will recursively merge properties from source objects into the destination. Objects and arrays are recursively merged. Other values will overwrite the value in the destination. This is similar to https://lodash.com/docs/4.17.4#merge | ||||
---|---|---|---|---|---|
Syntax |
. | ||||
Example | Expression: Input:
Result:
|
values
Description | Returns an array containing the given object's own enumerable property values. See also entries, keys. | ||
---|---|---|---|
Syntax |
| ||
Example | Input: Code Block | age
Expression: $user.values() |
Example Use Cases
Expand | |||||
---|---|---|---|---|---|
| |||||
|
...
Expand | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||
A JSON Array of objects with unique/non-overlapping keys can be converted to an object using the extend object method along with a Spread Operator. The extend object method does so by creating objects dynamically from other objects passed in as arguments, the spread operator will have to be used to indicate that the elements of the array should be treated as the arguments to the function. The expression to be used is:
1. Input the array into Mapper Snap (this can be done using a JSON Generator Snap). For this example, the following array will be used:
2. The Mapper Snap must be configured as shown below. Notice the Expression and Target path fields.3. When executed, the output will be an object.
|