bag_pack()

Learn how to use the bag_pack() function to create a dynamic JSON object from a list of keys and values.

Creates a dynamic property bag object from a list of keys and values.

Syntax

bag_pack(key1, value1, key2, value2,... )

Parameters

NameTypeRequiredDescription
keystring✔️The key name.
valueany scalar data type✔️The key value.

Returns

Returns a dynamic property bag object from the listed key and value inputs.

Examples

Example 1

The following example creates and returns a property bag from an alternating list of keys and values.

print bag_pack("Level", "Information", "ProcessID", 1234, "Data", bag_pack("url", "www.bing.com"))

Results

print_0
{“Level”: “Information”, “ProcessID”: 1234, “Data”: {“url”:“www.bing.com”}}

Example 2

The following example creates a property bag and extract value from property bag using ‘.’ operator.

datatable (
    Source: int,
    Destination: int,
    Message: string
) [
    1234, 100, "Hi", 
    4567, 200, "Hello",
    1212, 300, "Hey" 
]
| extend MyBag=bag_pack("Dest", Destination, "Text", Message)
| project-away Source, Destination, Message
| extend MyBag_Dest=MyBag.Dest, MyBag_Text=MyBag.Text

Results

MyBagMyBag_DestMyBag_Text
{“Dest”:100,“Text”:“Hi” }100Hi
{“Dest”:200,“Text”:“Hello” }200Hello
{“Dest”:300,“Text”:“Hey” }300Hey

Example 3

The following example uses two tables, SmsMessages and MmsMessages, and returns their common columns and a property bag from the other columns. The tables are created ad-hoc as part of the query.

SmsMessages

SourceNumberTargetNumberCharsCount
555-555-1234555-555-121246
555-555-1234555-555-121350
555-555-1212555-555-123432

MmsMessages

SourceNumberTargetNumberFileSizeFileTypeFileName
555-555-1212555-555-1213200jpegPic1
555-555-1234555-555-1212250jpegPic2
555-555-1234555-555-1213300pngPic3
let SmsMessages = datatable (
    SourceNumber: string,
    TargetNumber: string,
    CharsCount: string
) [
    "555-555-1234", "555-555-1212", "46", 
    "555-555-1234", "555-555-1213", "50",
    "555-555-1212", "555-555-1234", "32" 
];
let MmsMessages = datatable (
    SourceNumber: string,
    TargetNumber: string,
    FileSize: string,
    FileName: string
) [
    "555-555-1212", "555-555-1213", "200", "Pic1",
    "555-555-1234", "555-555-1212", "250", "Pic2",
    "555-555-1234", "555-555-1213", "300", "Pic3"
];
SmsMessages 
| join kind=inner MmsMessages on SourceNumber
| extend Packed=bag_pack("CharsCount", CharsCount, "FileSize", FileSize, "FileName", FileName) 
| where SourceNumber == "555-555-1234"
| project SourceNumber, TargetNumber, Packed

Results

SourceNumberTargetNumberPacked
555-555-1234555-555-1213{“CharsCount”:“50”,“FileSize”:“250”,“FileName”:“Pic2”}
555-555-1234555-555-1212{“CharsCount”:“46”,“FileSize”:“250”,“FileName”:“Pic2”}
555-555-1234555-555-1213{“CharsCount”:“50”,“FileSize”:“300”,“FileName”:“Pic3”}
555-555-1234555-555-1212{“CharsCount”:“46”,“FileSize”:“300”,“FileName”:“Pic3”}