如何使用 Kusto 解析字符串中的嵌套 JSON
How to parse nested JSON, within a string, using Kusto
我有一个 Python Azure 函数,它在函数执行时生成自定义日志消息。我需要提取一些嵌套在日志消息中的 JSON 值。
如何使用 Kusto 访问日志消息字符串中的 嵌套 JSON?
示例日志消息:
- 所需值标有
<----------
####### EventGrid trigger processing an event:
{
"id": "long-guid",
"data": {
"api": "FlushWithClose",
"requestId": "long-guid",
"eTag": "long-guid",
"contentType": "application/octet-stream",
"contentLength": 16264, <----------------------
"contentOffset": 0,
"blobType": "BlockBlob",
"blobUrl": "https://function.blob.core.windows.net/parentdir/childdir/file.name",
"url": "https://function.dfs.core.windows.net/parentdir/childdir/file.name", <---- JUST FILE.NAME here
"sequencer": "long-guid",
"identity": "long-guid",
"storageDiagnostics": {
"batchId": "long-guid"
}
},
"topic": "/subscriptions/long-guid/resourceGroups/resourceGroup/providers/Microsoft.Storage/storageAccounts/accountName",
"subject": "/blobServices/default/containers/containerName/blobs/childDir/file.name",
"event_type": "Microsoft.Storage.BlobCreated"
} #######
我想它与 Kusto extend
函数有关,但是管道...
| extend parsedMessage = todynamic(message)
| project timestamp, test = parsedMessage["id"]
...只产生一个空的 test
列
message
在您的特定情况下不是有效的 JSON 有效载荷 - 因为它具有 ###... EventGrid trigger processing an event:
前缀(和有点类似的后缀)。
- 这就是为什么
todynamic()
无法处理它以及为什么您无法引用其中包含的 JSON 有效负载中的属性的原因。
理想情况下,您会将摄取的有效载荷更改为有效的 JSON 有效载荷,然后将目标列重新键入 dynamic
而不是 string
。
如果你做不到,你可以使用substring()
函数或parse
运算符来获取除上述prefix/suffix之外的所有内容,并解析[=24]的输出=] 使用 todynamic()
- 但请注意,每次查询数据时都这样做会产生运行时开销,按照上述建议可以避免这些开销。
我有一个 Python Azure 函数,它在函数执行时生成自定义日志消息。我需要提取一些嵌套在日志消息中的 JSON 值。
如何使用 Kusto 访问日志消息字符串中的 嵌套 JSON?
示例日志消息:
- 所需值标有
<----------
####### EventGrid trigger processing an event:
{
"id": "long-guid",
"data": {
"api": "FlushWithClose",
"requestId": "long-guid",
"eTag": "long-guid",
"contentType": "application/octet-stream",
"contentLength": 16264, <----------------------
"contentOffset": 0,
"blobType": "BlockBlob",
"blobUrl": "https://function.blob.core.windows.net/parentdir/childdir/file.name",
"url": "https://function.dfs.core.windows.net/parentdir/childdir/file.name", <---- JUST FILE.NAME here
"sequencer": "long-guid",
"identity": "long-guid",
"storageDiagnostics": {
"batchId": "long-guid"
}
},
"topic": "/subscriptions/long-guid/resourceGroups/resourceGroup/providers/Microsoft.Storage/storageAccounts/accountName",
"subject": "/blobServices/default/containers/containerName/blobs/childDir/file.name",
"event_type": "Microsoft.Storage.BlobCreated"
} #######
我想它与 Kusto extend
函数有关,但是管道...
| extend parsedMessage = todynamic(message)
| project timestamp, test = parsedMessage["id"]
...只产生一个空的 test
列
message
在您的特定情况下不是有效的 JSON 有效载荷 - 因为它具有 ###... EventGrid trigger processing an event:
前缀(和有点类似的后缀)。
- 这就是为什么
todynamic()
无法处理它以及为什么您无法引用其中包含的 JSON 有效负载中的属性的原因。
理想情况下,您会将摄取的有效载荷更改为有效的 JSON 有效载荷,然后将目标列重新键入 dynamic
而不是 string
。
如果你做不到,你可以使用substring()
函数或parse
运算符来获取除上述prefix/suffix之外的所有内容,并解析[=24]的输出=] 使用 todynamic()
- 但请注意,每次查询数据时都这样做会产生运行时开销,按照上述建议可以避免这些开销。