如何在 Kusto 中将 JSON 转换为键值 table
How to convert JSON to key value table in Kusto
我有一个 table,它由一行和多列组成。其中一列名为 EventProperties
,它是 JSON 这种格式的属性:
{
"Success":true,
"Counters":{
"Counter1":1,
"Counter2":-1,
"Counter3":5,
"Counter4":4,
}
}
我想将 Counters
从此 JSON 转换为两列 table 键和值,其中第一列是计数器的名称(例如 Counter3 ),第二列是计数器的值(例如 5)。
我试过这个:
let eventPropertiesCell = materialize(MyTable
| project EventProperties
);
let countersStr = extractjson("$.Counters", tostring(toscalar(eventPropertiesCell)), typeof(string));
let countersJson = parse_json(countersStr);
let result =
print mydynamicvalue = todynamic(countersJson)
| mvexpand mydynamicvalue
| evaluate bag_unpack(mydynamicvalue);
result
但是我得到一个 table,其中每个计数器都有一列来自 JSON,行数等于计数器的数量,而只有一个随机行填充了计数器值。例如,使用上面示例中的 JSON,我得到:
但我想要这样的东西:
任何帮助将不胜感激!
您可以尝试使用 mv-apply
如下:
datatable(event_properties:dynamic)
[
dynamic({
"Success":true,
"Counters":{
"Counter1":1,
"Counter2":-1,
"Counter3":5,
"Counter4":4
}
}),
dynamic({
"Success":false,
"Counters":{
"Counter1":1,
"Counter2":2,
"Counter3":3,
"Counter4":4
}
})
]
| mv-apply event_properties.Counters on (
extend key = tostring(bag_keys(event_properties_Counters)[0])
| project key, value = event_properties_Counters[key]
)
| project-away event_properties
我有一个 table,它由一行和多列组成。其中一列名为 EventProperties
,它是 JSON 这种格式的属性:
{
"Success":true,
"Counters":{
"Counter1":1,
"Counter2":-1,
"Counter3":5,
"Counter4":4,
}
}
我想将 Counters
从此 JSON 转换为两列 table 键和值,其中第一列是计数器的名称(例如 Counter3 ),第二列是计数器的值(例如 5)。
我试过这个:
let eventPropertiesCell = materialize(MyTable
| project EventProperties
);
let countersStr = extractjson("$.Counters", tostring(toscalar(eventPropertiesCell)), typeof(string));
let countersJson = parse_json(countersStr);
let result =
print mydynamicvalue = todynamic(countersJson)
| mvexpand mydynamicvalue
| evaluate bag_unpack(mydynamicvalue);
result
但是我得到一个 table,其中每个计数器都有一列来自 JSON,行数等于计数器的数量,而只有一个随机行填充了计数器值。例如,使用上面示例中的 JSON,我得到:
但我想要这样的东西:
任何帮助将不胜感激!
您可以尝试使用 mv-apply
如下:
datatable(event_properties:dynamic)
[
dynamic({
"Success":true,
"Counters":{
"Counter1":1,
"Counter2":-1,
"Counter3":5,
"Counter4":4
}
}),
dynamic({
"Success":false,
"Counters":{
"Counter1":1,
"Counter2":2,
"Counter3":3,
"Counter4":4
}
})
]
| mv-apply event_properties.Counters on (
extend key = tostring(bag_keys(event_properties_Counters)[0])
| project key, value = event_properties_Counters[key]
)
| project-away event_properties