構造化文字列データからデータを抽出する

完了

文字列フィールドには、JSON やキーと値のペアのような構造化データを含めることもできます。 KQL を使用すれば、これらの値に簡単にアクセスして詳細な分析を行うことができます。

動的フィールド

Log Analytics テーブル内に、動的として定義されているフィールド型があります。 動的フィールドには、次のようなキーと値のペアが含まれます。

{"eventCategory":"Autoscale","eventName":"GetOperationStatusResult","operationId":"xxxxxxxx-6a53-4aed-bab4-575642a10226","eventProperties":"{\"OldInstancesCount\":6,\"NewInstancesCount\":5}","eventDataId":" xxxxxxxx -efe3-43c2-8c86-cd84f70039d3","eventSubmissionTimestamp":"2020-11-30T04:06:17.0503722Z","resource":"ch-appfevmss-pri","resourceGroup":"CH-RETAILRG-PRI","resourceProviderValue":"MICROSOFT.COMPUTE","subscriptionId":" xxxxxxxx -7fde-4caf-8629-41dc15e3b352","activityStatusValue":"Succeeded"}

動的フィールド内の文字列にアクセスするには、ドット表記を使用します。 SigninLogs テーブルの DeviceDetail フィールドの型は動的です。 この例では、DeviceDetail.operatingSystem というフィールド名を使用してオペレーティング システムにアクセスできます。

SigninLogs 
| extend OS = DeviceDetail.operatingSystem

次のクエリ例では、SigninLogs テーブルでの動的フィールドの使用を示します。

// Example query for SigninLogs showing how to break out packed fields.

SigninLogs 
| extend OS = DeviceDetail.operatingSystem, Browser = DeviceDetail.browser 
| extend StatusCode = tostring(Status.errorCode), StatusDetails = tostring(Status.additionalDetails) 
| extend Date = startofday(TimeGenerated) 
| summarize count() by Date, Identity, UserDisplayName, UserPrincipalName, IPAddress, ResultType, ResultDescription, StatusCode, StatusDetails 
| sort by Date

JSON

KQL には、文字列フィールドに格納されている JSON を操作する関数が用意されています。 多くのログでは JSON 形式でデータを送信します。そのため、JSON データをクエリ可能なフィールドに変換する方法を知る必要があります。

下の例は、JSON に関連する関数と操作の一覧です。

Function 説明
parse-json() または todynamic() 文字列が JSON 値として解釈され、値が dynamic として返されます。 フィールドを参照するには、次のいずれかの関数を使用します。JsonField.Key または JsonField["Key"]
mv-expand は、dynamic 型の配列またはプロパティ バッグ列に適用され、コレクション内の各値が個別の行になります。 展開された行内のその他の列はすべて複製されます。 mv_expand は、JSON 配列を処理する最も簡単な方法です。
mv-apply サブクエリを各レコードに適用し、全サブクエリの結果を和集合にしたものを返します。 配列の各値にクエリを適用します。

各クエリを個別に実行して、結果を確認します。

SigninLogs 
| extend AuthDetails =  parse_json(AuthenticationDetails) 
| extend AuthMethod =  AuthDetails[0].authenticationMethod 
| extend AuthResult = AuthDetails[0].["authenticationStepResultDetail"] 
| project AuthMethod, AuthResult, AuthDetails 


SigninLogs 
| mv-expand AuthDetails = parse_json(AuthenticationDetails) 
| project AuthDetails

SigninLogs 
| mv-apply AuthDetails = parse_json(AuthenticationDetails) on
(where AuthDetails.authenticationMethod == "Password")