Azure Application Insights如何提取自定义维度嵌套属性至独立列
提取日志中Properties内的独立列(不受键顺序影响)
问题背景
我的日志的customDimensions中包含多个条目,示例如下:
{"MessageType":"EventLog","Properties":"{\"Action\":\"Manual Trigger\",\"City\":\"New York\"}","AppBuild":"22"}
需要将"Properties"内的条目提取为独立列,期望效果如下:
| Action | City |
|---|---|
| Manual Trigger | New York |
但"Properties"内的键值对顺序不固定,有时City会排在Action前面。目前已能提取Properties字段,但按顺序拆分的方式因顺序不稳定无法使用。现有提取语句如下:
let events = customEvents | extend properties = tostring(customDimensions["Properties"])
或
let events = customEvents | extend properties = tostring(customDimensions.Properties)
解决方案
使用Kusto的parse_json()函数将Properties字符串解析为动态JSON对象,之后直接按字段名提取列,完全不受键顺序影响。
基础版查询
let events = customEvents | extend properties = parse_json(tostring(customDimensions.Properties)) | extend Action = tostring(properties.Action), City = tostring(properties.City) | project Action, City // 按需保留或添加其他列
带默认值的增强版(处理字段缺失场景)
如果Properties内的字段可能存在缺失,可通过coalesce()设置默认值:
let events = customEvents | extend properties = parse_json(tostring(customDimensions.Properties)) | extend Action = coalesce(tostring(properties.Action), "N/A"), City = coalesce(tostring(properties.City), "N/A") | project Action, City
关键说明
parse_json():将字符串格式的JSON转换为动态对象,支持通过.直接访问内部键值tostring():将提取的字段转为字符串类型,若字段是数字/日期等类型,可改用tolong()/todatetime()等对应转换函数coalesce():返回第一个非空值,避免缺失字段导致的空值问题
内容的提问来源于stack exchange,提问作者user17387398
相关产品推荐
相关产品推荐

