-
Notifications
You must be signed in to change notification settings - Fork 6
OBJECT.MERGE
OBJECT.MERGE
merges two or more objects. If the same key is present multiple times, the rightmost value will be used.
OBJECT.MERGE(arg1, [arg2...])
-
arg1
is an object or key/value pair. -
OBJECT.MERGE
can accept an arbitrary number of objects or key/value pairs - Any argument can also contain a dynamic reference to the current data context, which is accessed with the
_
character.
Let's say we're given a response with some vehicle performance information that looks like this:
{
"data": {
"vin": "3VWJP7AT9CM624721",
"vehicle": {
"fuel_efficiency": 95,
"breaking_time": 2400
},
"fleets": ["alpha", "bravo", "charlie"],
},
"active_fleet": {
"active": true,
"name": ""
}
}
If we want to merge the vehicles VIN into the vehicle
hash, we can used the OBJECT.MERGE
function like this:
OBJECT.MERGE(data.vehicle, {{"VIN", data.vin}})
In the above case, the first argument is a hash referenced in the context.
This would return:
{ "VIN": "3VWJP7AT9CM624721", "fuel_efficiency": 95, "breaking_time": 2400 }
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, we could do it as seen below:
MAP(OBJECT.MERGE({{data.active_fleet, _}}, MAP(OBJECT.NEW({{"name", _}}), data.fleets))
This would return:
[
{ "active": true, "name": "alpha" },
{ "active": true, "name": "bravo" },
{ "active": true, "name": "charlie" }
]