OBJECT.MERGE

OBJECT.MERGE merges two or more objects. If the same key is present multiple times, the rightmost value will be used. This function can accept an arbitrary number of objects or key/value pairs.

Function category: Object

Syntax

OBJECT.MERGE(arg1, [arg2...])

Arguments

Description

arg1

Object or key/value pair.

arg2

Optional. Has the same restrictions as arg1.

Any argument can also contain a dynamic reference to the current data context, which is accessed with the _ character.

Examples

Let's say we're given a response with the following vehicle performance information.

{
"data": {
"vin": "3VWJP7AT9CM624721",
"vehicle": {
"fuel_efficiency": 95,
"breaking_time": 2400
},
"fleets": ["alpha", "bravo", "charlie"],
},
"active_fleet": {
"active": true,
"name": ""
}
}

Example 1: Merge objects into a hash

If we want to merge the vehicles VIN into the vehicle hash, use the following function.

# Merge objects into a hash
OBJECT.MERGE(data.vehicle, {{"VIN", data.vin}})
# the first argument data.vehicle
# is a hash referenced in the context

This returns the following.

{ "VIN": "3VWJP7AT9CM624721", "fuel_efficiency": 95, "breaking_time": 2400 }

Example 2: Using the dynamic reference character ( _ )

It is also possible to employ the dynamic reference character in using OBJECT.MERGE. For instance, if we wanted to generate a set of objects describing fleet activity status, use the following function.

# Generate objects using dynamic reference character
MAP(OBJECT.MERGE(FN(x, {{data.active_fleet, x}}), MAP(FN(x, OBJECT.NEW({{"name", x}})), data.fleets))

This returns the following.

[
{ "active": true, "name": "alpha" },
{ "active": true, "name": "bravo" },
{ "active": true, "name": "charlie" }
]