diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_endpoints.c b/src/mod/event_handlers/mod_kazoo/kazoo_endpoints.c index cf253760ed..bec5b5e1ea 100644 --- a/src/mod/event_handlers/mod_kazoo/kazoo_endpoints.c +++ b/src/mod/event_handlers/mod_kazoo/kazoo_endpoints.c @@ -340,17 +340,7 @@ static switch_call_cause_t kz_endpoint_outgoing_channel(switch_core_session_t *s // add runtime vars to event for expand if (ctx) { - cJSON *item = NULL; - char *response = NULL; - cJSON_ArrayForEach(item, ctx) { - if (item->type == cJSON_String) { - response = strdup(item->valuestring); - } else { - response = cJSON_PrintUnformatted(item); - } - kz_switch_event_add_variable_name_printf(event, SWITCH_STACK_BOTTOM, response, "kz_ctx_%s", item->string); - switch_safe_free(response); - } + kz_expand_json_to_event(ctx, event, "kz_ctx"); } d_dest = kz_event_expand_headers(event, dest); diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_utils.c b/src/mod/event_handlers/mod_kazoo/kazoo_utils.c index afc685434f..34b33c8ae1 100644 --- a/src/mod/event_handlers/mod_kazoo/kazoo_utils.c +++ b/src/mod/event_handlers/mod_kazoo/kazoo_utils.c @@ -516,6 +516,32 @@ SWITCH_DECLARE(switch_status_t) kz_switch_event_add_variable_name_printf(switch_ return status; } +SWITCH_DECLARE(switch_status_t) kz_expand_json_to_event(cJSON *json, switch_event_t *event, char * prefix) +{ + char * fmt = switch_mprintf("%s%s%%s", prefix ? prefix : "", prefix ? "_" : ""); + if (event) { + cJSON *item = NULL; + char *response = NULL; + cJSON_ArrayForEach(item, json) { + if (item->type == cJSON_String) { + response = strdup(item->valuestring); + } else if (item->type == cJSON_Object) { + char * fmt1 = switch_mprintf(fmt, item->string); + kz_expand_json_to_event(item, event, fmt1); + switch_safe_free(fmt1); + continue; + } else { + response = cJSON_PrintUnformatted(item); + } + kz_switch_event_add_variable_name_printf(event, SWITCH_STACK_BOTTOM, response, fmt, item->string); + switch_safe_free(response); + } + } + + switch_safe_free(fmt); + return SWITCH_STATUS_SUCCESS; +} + SWITCH_DECLARE(switch_xml_t) kz_xml_child(switch_xml_t xml, const char *name) { xml = (xml) ? xml->child : NULL; diff --git a/src/mod/event_handlers/mod_kazoo/kazoo_utils.h b/src/mod/event_handlers/mod_kazoo/kazoo_utils.h index 397d94a361..c5ddc3e037 100644 --- a/src/mod/event_handlers/mod_kazoo/kazoo_utils.h +++ b/src/mod/event_handlers/mod_kazoo/kazoo_utils.h @@ -48,4 +48,6 @@ void kz_expand_headers_self(switch_event_t *event); char * kz_expand_vars_pool(char *xml_str, switch_memory_pool_t *pool); switch_status_t kz_json_api(const char * command, cJSON *args, cJSON **res); +SWITCH_DECLARE(switch_status_t) kz_expand_json_to_event(cJSON *json, switch_event_t *event, char * prefix); + KZ_END_EXTERN_C