KQL解析JSON数组异常:parse_json、todynamic均无法获取Username字段
问题根因
DeviceInfo表内置的LoggedOnUsers字段本身为用户对象数组结构,并非单个JSON对象。使用todynamic转换得到的AllProperties为数组类型,直接对数组执行["Username"]属性索引无法命中有效值,所以返回空结果。
修复方案
根据你的使用场景选择对应写法即可:
- 场景1:需要导出所有登录用户的用户名,每个用户单独对应一行记录
DeviceInfo | where RegistryDeviceTag == "Standard" | extend AllProperties = todynamic(LoggedOnUsers) | mv-expand AllProperties | project DeviceName, Users = AllProperties.Username
- 场景2:仅需要取第一条登录记录的用户名
DeviceInfo | where RegistryDeviceTag == "Standard" | extend AllProperties = todynamic(LoggedOnUsers) | project DeviceName, Users = AllProperties[0].Username
验证方法
你可以先运行以下查询确认字段结构,若gettype返回值为array即可确认上述问题判断准确:
DeviceInfo | where RegistryDeviceTag == "Standard" | extend AllProperties = todynamic(LoggedOnUsers) | project AllProperties, field_type = gettype(AllProperties)
内容的提问来源于stack exchange,提问作者John418
相关产品推荐
相关产品推荐

