Target filter
Search Kotlin docs
  • Platforms
  • Tealium Prism Kotlin
core/com.tealium.prism.core.api.data/DataObject/merge

merge

@JvmOverloads
fun merge(other: DataObject, depth: Int = Int.MAX_VALUE): DataObject

Deep merges two DataObjects together, returning a new DataObject containing the merged data.

This method will merge data according to the following rules

  • DataObjects - will have all keys from this and other in the returned DataObject

  • DataLists - will always prefer the value in the other object if present. DataList contents are never merged.

The depth parameter controls how many levels of DataObject nesting should be merged. After the given depth, if a DataObject is found in both this and other objects, then the latter is chosen in its entirety.

Example

val lhs = DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-1", DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-2", DataObject.create {
put("key1", "string")
put("key2", true)
put("lvl-3", DataObject.create {
put("key1", "string")
put("key2", true)
})
})
})
}

val rhs = DataObject.create {
put("key1", "new string")
put("lvl-1", DataObject.create {
put("key1", "new string")
put("lvl-2", DataObject.create {
put("key1", "new string")
put("key3", 1)
put("lvl-3", DataObject.create {
put("key1", "new string")
})
})
})
}

val merged = lhs.merge(rhs, 2)

// merged will be the equivalent of this:
DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("lvl-1", DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("lvl-2", DataObject.create {
put("key1", "new string") // from rhs
put("key2", true) // from lhs
put("key3", 1) // from rhs
put("lvl-3", DataObject.create {
put("key1", "new string") // from rhs only
})
})
})
}
Content copied to clipboard

The default value for depth is Int.MAX_VALUE, and will deep merge all levels. Zero or negative values will be equivalent to the DataObject.plus operator.

Parameters

other

The incoming object, whose key/values are to merged into the current object.

depth

Optional limit on the number of levels deep to merge.

Generated by Dokka
(c) Tealium 2026