Home / Search/ Language Reference/ Functions/ Scalar Functions/ Dynamic Functions/bag_set_key

bag_set_key

The bag_set_key function adds a key-value pair to a property bag, or, if the specified key already exists, overwrites the key’s value.

Syntax

     bag_set_key( Bag, Key, Value )

Parameters

NameTypeRequiredDescription
BagdynamicYesThe property bag to modify.
KeystringYesThe key to set.
ValuedynamicYesThe value to set.

Returns

Returns the input property bag, adding the specified key-value pair if the Key doesn’t exist, or overwriting the value if the Key is already there.

The order of the key-value pairs returned is undetermined.

If the Bag specified is not a valid property bag, bag_set_key returns null.

Examples

Add a Key-Value Pair to a Property Bag

print bag = dynamic({ "foo": 42, "bar": true })
 | project result = bag_set_key(bag, "new", 1)

Input (before bag_set_key):

{
  "bag": {
    "foo": 42,
    "bar": true
  }
}

Output (after bag_set_key):

{
  "result": {
    "foo": 42,
    "bar": true,
    "new": 1
  }
}

Overwrite a Key-Value Pair in a Property Bag

print bag = dynamic({ "foo": 42, "bar": true })
 | project result = bag_set_key(bag, "foo", 100)

Input (before bag_set_key):

{
  "bag": {
    "foo": 42,
    "bar": true
  }
}

Output (after bag_set_key):

{
  "result": {
    "foo": 100,
    "bar": true
  }
}