Skip to main content

SpEL Examples

Objects and Properties

Add multiple properties to an object

Be careful when assigning new value to an object. If you assign a new object to a property, the old object will be replaced with the new one. If you want to add new properties to an existing object, you need to add single property or merge the new object with the existing one.

Replace the whole object

#do(
// You have an object with some data
#entity = {
data: {
one: 1,
two: 2
}
},

// replace the object data with new value
#entity.data = {
three: 3
},

#entity
)

Result:

{
"data": {
"three": 3
}
}

Add / replace several properties

#do(
// You have an object with some data
#entity = {
data: {
one: 1,
two: 2
}
},

// Add new property
#entity.data.three = 3,

// Add more properties from object
// Leave existing, replace if the same key exists
#entity.data = #entity.data + {
one: 11,
four: 4
},

#entity
)

Result:

{
"data": {
"one": 11,
"two": 2,
"three": 3,
"four": 4
}
}