Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated note in hasOwnProperty for PLAT-6559

In this Page

Table of Contents
maxLevel2
absoluteUrltrue
excludeAdditional Resources|Related Links|Related Information

Object Literals

In this Page

Table of Contents
maxLevel2
absoluteUrltrue
excludeAdditional Resources|Related Links|Related Information

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:

Code Block
{
    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:

Code Block
[expression] : value

The result of the expression will be converted into a string.

Example


Code Block
{
    "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

...

Returns an array of the given object's own enumerable property [key,value] pairs. See also keys, values.

...

Code Block
property.entries()

...

Input:

Code Block
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. 
 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


Code Block
property.entries()


Example

Input:

Code Block
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. 

Note

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


Code Block
object.extend(target:data)


Example

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

Result:

Code Block
{
      "newField1": "foo",
      "newField2": "bar"
}


Note

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

object.extend(target:data

See also: Checking for optional properties and returning defaults in the expression language

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.

Note

This function is not supported in Spark 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


Code Block
Syntax


object.get(field, [defaultValue])


Example

Expression: $.

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

Result:

Code Block
{
      "newField1": "foo",
      "newField2": "bar"
}
Note

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

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.

Description
Note

This function is not supported in Spark pipelines.

See also: Checking for optional properties and returning defaults in the expression language

Syntaxobject.get(field, [defaultValue]

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.

getFirst

Description

Find the value for the given property 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


object.getFirst(propertyName, defaultValue)


Example

Expression$.getgetFirst("Idtest")Result: Returns the value

of the "Id" property or null if the object does not have the property.Where $test  is the string "abc123"

Result: abc123


Expression$.getgetFirst("Id", 123)

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

getFirst

...

Find the value for the given property 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.

...

object.getFirst(propertyName, defaultValue)

...

Expression$.getFirst("test")

Where $test  is the string "abc123"

Result: abc123

Expression$.getFirst("test")

Where $test is a list consisting of [5, 10, 15, 20]

Result: 5

hasOwnProperty

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. 

test")

Where $test is a list consisting of [5, 10, 15, 20]

Result: 5

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.

Note

This function is not supported in Spark pipelines.

Note


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.


Note

This argument is treated as a JSON-Path 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 JSON-Path 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:

$.map.hasOwnProperty("route")

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:

$.hasOwnProperty("map.route")

Where map.route is a path and not a property.

In this case, we recommend you use the hasPath() method.


Syntax


Code Block
object.hasOwnProperty(field)


Example

Expression: $.hasOwnProperty("Id")

Result: Returns true if the object has the key "Id"


Expression: To create a ternary conditional expression:

$.hasOwnProperty('query') ? $query : 'not present in input'

...

Expand
titleExtend Method: Converting an array into an object

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:


Paste code macro
titleExpression using extend method to convert an array into an object
{}extend(...$<Array_name>)

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:

Paste code macro
languagejson
titleJSON Array
{
 "myArray": [
   {
     "Header1": {
       "a": 11,
       "b": 21,
       "c": 31
     }
   },
   {
     "Header2": {
       "a": 12,
       "b": 22,
       "c": 32
     }
   },
   {
     "Header3": {
       "a": 13,
       "b": 23,
       "c": 33
     }
   }
 ]
}

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.

Note

The array should have unique keys for this method to work, if they overlap then the last element of the array will be converted.