Parsing nested `metrics` array in Genesys Cloud `v2.analytics.conversation.aggregate` webhook payload

Setting up a Kotlin backend service to consume Genesys Cloud webhooks. Specifically subscribing to the v2.analytics.conversation.aggregate event type. The goal is to extract specific conversation metrics like talk_duration and hold_duration for reporting.

The problem is the JSON structure of the data object in the webhook payload. It’s deeply nested and the documentation is a bit sparse on the exact schema for the metrics array within the aggregate event. Here’s a snippet of the payload I’m receiving:

{
 "data": {
 "conversationId": "abc-123-def",
 "metrics": [
 {
 "name": "talk_duration",
 "value": 120.5
 },
 {
 "name": "hold_duration",
 "value": 30.0
 }
 ]
 }
}

I’m using Moshi for JSON parsing in Kotlin. I’ve defined a data class for the outer structure, but I’m struggling with the metrics list. Since the metric names are dynamic strings, I can’t hardcode properties in the data class. I tried mapping it to a List<Map<String, Any>> but then I lose type safety on the value field. It comes back as a generic Any or JsonElement depending on how I configure the adapter.

Is there a standard way to handle this dynamic key-value structure in the metrics array? Or should I just iterate through the JsonElement manually? I’d prefer a clean data class approach if possible.

Also, noticed the value field is sometimes an integer and sometimes a double. Moshi complains about type mismatch if I define it as Double. Any tips on handling mixed numeric types in the value field?

The metrics aren’t nested arrays, they’re a flat object inside data. Check the actual payload structure:

{
 "data": {
 "metrics": {
 "talk_duration": 120,
 "hold_duration": 30
 }
 }
}

Access it directly via data.metrics.talk_duration.