如何使用 kusto 在 json 数组中查找项目

How to find an item in a json array using kusto

我有一个 json 数组记录为

[
  {
    "Key": "key0",
    "Value": 0
  },
  {
    "Key": "key1",
    "Value": 2
  }
]

如何为 Key 获得 Value 的值 key0,所以 0.

我一直在用这个kluge

...
| extend jsonarray = parse_json(...)
| extend value = toint(case(
    jsonarray[0].Key == 'key0', jsonarray[0].Value,
    jsonarray[1].Key == 'key0', jsonarray[1].Value,
    "<out-of-range>"))

更新:

使用mv-apply

| extend jsonarray = parse_json(...)
| mv-apply jsonarray on (
    where jsonarray.Key == 'key0'
    | project value = toint(jsonarray.Value)
    )

您可以使用 mv-expandmv-apply:

print d = dynamic([
  {
    "Key": "key0",
    "Value": 0
  },
  {
    "Key": "key1",
    "Value": 2
  }
])
| mv-apply d on (
    where d.Key == "key0"
    | project d.Value
)