bag_set_key()

bag_set_key() receives a dynamic property-bag, a key and a value. The function sets the given key in the bag to the given value. The function overrides any existing value in case the key already exists.

Syntax

bag_set_key(bag,key,value)

Learn more about syntax conventions.

Parameters

Name Type Required Description
bag dynamic ✔️ The property bag to modify.
key string ✔️ The key to set. Either a JSON path (you can specify a key on the nested levels using JSONPath notation) or the key name for a root level key. Array indexing or root JSON paths aren't supported.
value any scalar data type ✔️ The value to which the key is set.

Returns

Returns a dynamic property-bag with specified key-value pairs. If the input bag isn't a property-bag, a null value is returned.

Note

To treat nulls as empty bags, use coalesce(x, dynamic({}))​.

Examples

Use a root-level key

datatable(input: dynamic) [
    dynamic({'key1': 1, 'key2': 2}), 
    dynamic({'key1': 1, 'key3': 'abc'}),
]
| extend result = bag_set_key(input, 'key3', 3)
input result
{
"key1": 1,
"key2": 2
}
{
"key1": 1,
"key2": 2,
"key3": 3
}
{
"key1": 1,
"key3": "abc"
}
{
"key1": 1,
"key3": 3
}

Use a JSONPath key

datatable(input: dynamic)[
    dynamic({'key1': 123, 'key2': {'prop1': 123, 'prop2': 'xyz'}}),
    dynamic({'key1': 123})
]
| extend result = bag_set_key(input, '$.key2.prop1', 'abc')
input result
{
"key1": 123,
"key2": {
"prop1": 123,
"prop2": "xyz"
}
}
{
"key1": 123,
"key2": {
"prop1": "abc",
"prop2": "xyz"
}
}
{
"key1": 123
}
{
"key1": 123,
"key2": {
"prop1": "abc"
}
}