如何在 Kusto 中将 JSON 转换为键值表

2023-12-01

我有一张由一行和多列组成的表格。其中一列被命名为EventProperties这是以下格式的属性的 JSON:

{
   "Success":true,
   "Counters":{
      "Counter1":1,
      "Counter2":-1,
      "Counter3":5,
      "Counter4":4,
   }
}

我想转换Counters将此 JSON 转换为键和值的两列表,其中第一列是计数器的名称(例如 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

但我得到一张表,其中每个计数器从 JSON 中有一列,行数等于计数器的数量,而只有一个随机行填充了计数器值。例如,使用上面示例中的 JSON,我得到:

enter image description here

但我想要这样的东西:

enter image description here

任何帮助将不胜感激!


你可以尝试使用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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Kusto 中将 JSON 转换为键值表 的相关文章

随机推荐