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.

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$.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

Description

Returns a boolean value to indicate 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 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:

$.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


object.hasOwnProperty(field)


Example

Expression: $.hasOwnProperty("route") where $ object corresponds to my.custom.route

Result: Returns true.


Expression: $.hasOwnProperty("custom.route") where $ object corresponds to my.custom.route

Result:

  • Returns false if the value of route is null as custom.route is not a property but a path. (Reason to use hasPath() function instead.)
  • Returns true if the value of route is not null though custom.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.

Instead of chaining multiple sub-expressions using the hasOwnProperty method to validate the subpath at each node of the JSONPath, you can call hasPath only once to do the same validation. You must provide the JSONPath to the property.


Syntax


object.hasPath(JSONPath.to.property)


Example

Expression: $.hasPath("route") where $ object corresponds to my.custom.route

Result: Returns true.


Expression: $.hasPath("custom.route") where $ object corresponds to my.custom.route

Result:

  • Returns false if the value of route is null.
  • Returns true if the value of route is not null.


Example JSON Object:

{
  "Name":"previewdata.json",
  "Type":"File",
  "Size (in bytes)":"4",
  "Owner":null,
  "Update date":"2019-10-06T16:50:58.000Z",
  "Editor": {
    "EditorName": "John Doe",
    "Dept": "Marketing"
  }
  "Creation date":"unknown",
  "Permissions":"unknown"
}

Expression$.hasPath("Type")

Result: Returns true because the object "Type" has a specified value. 

Expression$.hasPath($.Editor.EditorName)

Result: Returns true because "EditorName" has a specified value. 

Expression: $.hasPath("Owner")

Result: Returns false since even though the object has the key "Owner", it is null.

Expression: $.hasPath("Region")

Result: Returns false since the object does not have "Region".

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

DescriptionCreate a new object that retains some properties from the original as specified by the given callback.
Syntax


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.
Example

Expression: $.filter((value, key) => key.startsWith("new"))

Input:

{
      "key1": "abc",
      "key2": "xyz",
	  "newField1": "foo",
      "newField2": "bar"
    }

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

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)
  • callback - A function that takes three arguments (property-value, property-name, input-object) and returns the new value for the property key.
Example

Expression: $.mapKeys((value, key) => "new" + key)

Input:

{
      "Field1": "foo",
      "Field2": "bar"
    }

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)
  • callback - A function that takes three arguments (property-value, property-name, input-object) and returns the new value for the property.
Example

Expression: $.mapValues((value, key) => key == "newField1" ? "foo" : "bar")

Input:

{
      "newField1": "abc",
      "newField2": "xyz"
    }

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: $.merge({child: {age: 32}})

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:

[{“user”: {
    “name”: "John",
    “age”: 30
    }
}]

Expression: $user.values()
Result: 
["John", 30]

Example Use Cases


Expression


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


DescriptionThis example is one way of doing an if-then-else expression.



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:


{}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:

{
 "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.

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