Object Functions and Properties

Object Functions and Properties

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:

  • this - A reference to the current object that can be used to reference previously defined properties.

  • __parent__ - A reference to the parent object when used within a nested object literal.

  • __root__ - A reference to the top-level object when used within a deeply nested object literal.

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()
Result: [ ["name","John"], ["age",30] ]

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

object.extend(target:data)

Example

Expression: $.extend({ newField1 : 'foo' }, { newField2 : 'bar' })

Input:

{}

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

object.get(field, [defaultValue])

Example

Expression$.get("Id")

Result: Returns the value of the "Id" property or null if the object does not have the property.

 

Expression$.get("Id", 123)

Result: Returns the value of the "Id" property or the number "123" if the object does not have the property.