dynamic columns and parse_json()

Some log columns hold JSON instead of a single value. Kusto calls their type dynamic. In the tables here, LocationDetails (SigninLogs), InitiatedBy and TargetResources (AuditLogs) are dynamic:

{"city":"Prague","countryOrRegion":"CZ","geoCoordinates":{"latitude":50.08,"longitude":14.44}}

Reading values

  • Dot notation reads a property: LocationDetails.city.
  • Chain it for nested objects: LocationDetails.geoCoordinates.latitude.
  • Brackets read list items by position, from 0: TargetResources[0]. [-1] is the last item.
  • Bracket notation also works for keys: LocationDetails["city"], needed when a key has spaces or dashes.
  • A property that does not exist gives an empty (null) value, not an error. Keys are case-sensitive.

Convert before you use it

What you read is still dynamic. Before comparing, grouping or sorting, convert it: tostring(LocationDetails.city) == "Prague", todouble(...latitude). See type conversion.

parse_json()

Sometimes JSON arrives as plain text, inside a string column. parse_json(text) (also called todynamic()) turns that text into a dynamic value you can read with the same notation. In real Kusto, that works for any JSON. On this site, parse_json() handles only JSON strings (text in double quotes), as used inside some audit log properties; reading into an object or list made with parse_json() is refused with a clear message. The dynamic columns themselves can always be read directly.

Things to know

  • split() and make_set() also produce dynamic lists; the same index notation works on them.
  • On this site, mv-expand (one row per list item) is not supported yet.

Examples

1 A property of a JSON column
SigninLogs
| extend City = tostring(LocationDetails.city)
| summarize SignIns = count() by Location, City
| top 5 by SignIns
2 Nested properties and the first item of a list
AuditLogs
| where OperationName == "Register device"
| extend Who = tostring(InitiatedBy.user.userPrincipalName), Device = tostring(TargetResources[0].displayName)
| project TimeGenerated, Who, Device
3 Bracket notation and a number
SigninLogs
| extend Lat = todouble(LocationDetails["geoCoordinates"]["latitude"])
| summarize Places = dcount(Lat) by Location
| sort by Places desc

Practice it

None of the free exercises needs this one yet.