In this Page
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: { property-name1 : value1, property-name2 : value2, ... property-nameN : valueN, } The property name can be computed dynamically by enclosing an expression in square brackets ([]), like so: [expression] : value The result of the expression will be converted into a string. |
Example | { "msg": "Hello, World!", /* Unlike JSON, property names do not need to be quoted */ num: 123, /* Property names can be computed using an expression inside square brackets */ [2 * 2]: "four", /* Other fields in this object can be referenced using the 'this' variable */ ref: this.num + 7 /* sets 'ref' equal to 130 (123 + 7) */ } |
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 | property.entries() |
Example | Input: let user = { name: "John", age: 30 } 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. This function is not supported in Spark pipelines. 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 | object.extend(target:data) |
Example | Expression: $.extend({ newField1 : 'foo' }, { newField2 : 'bar' }) Result: { "newField1": "foo", "newField2": "bar" } When using the above expression in a Mapper ensure that the input stream is not empty else a null value error will be shown. If an empty document is to be created then use {} instead of $: {}.extend({ newField1 : 'foo' },{ newField2 : 'bar' }) |
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. This function is not supported in Spark pipelines. 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. |
hasOwnProperty
Description | Indicates whether the object has the specified property. 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. This function is not supported in Spark pipelines. The function does not currently match the JavaScript behavior and will be changed in the near future. In particular, the argument is treated as a JSON-Path instead of a plain property name and it will return false if the property exists, but the value is null. Most expressions should continue to work as expected since they are not qualified paths. If your expression passes a JSON-Path or if the property value is null, use the hasPath method instead. |
---|---|
Syntax | object.hasOwnProperty(field) |
Example | Expression: Result: Returns true if the object has the key "Id" Expression: To create a ternary conditional expression:
|
hasPath
Description | Indicates whether the object has the specified property. This method is recommended to be used instead of the hasOwnProperty method when working with JSON-Path, especially when looking for fields nested deep within the object. This is handy since if you use the hasOwnProperty method, you would have to construct complex sub-expressions to check if the field exists. |
---|---|
Syntax | |
Example | Expression: Result: Returns true if the object has the key "Id" |
isEmpty
Description | Returns true if the given object has no properties. This function is not supported in Spark pipelines. |
---|---|
Syntax | object.isEmpty() |
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 | object.filter(callback)
|
Example | Expression: $.filter((value, key) => key.startsWith("new")) Result: { "newField1": "foo", "newField2": "bar" } |
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 | property.keys() |
Example | Input: let user = { name: "John", age: 30 } 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 | object.mapKeys(callback)
|
Example | Expression: Result: { "newField1": "foo", "newField2": "bar" } |
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 | object.mapValues(callback)
|
Example | Expression: Result: { "newField1": "foo", "newField2": "bar" } |
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 | object.merge(obj1, ..., objN) . |
Example | Expression: Input: { "id": 12345, "child": { "name": "John Doe" } } Result: { "id": 12345, "child": { "name": "John Doe", "age": 32 } } |
values
Description | Returns an array containing the given object's own enumerable property values. See also entries, keys. |
---|---|
Syntax | property.values() |
Example | Input: let user = { name: "John", age: 30 } Expression: $user.values() |