src/aws/flb_aws_util.c
flb_sds_t flb_json_get_val(char *response, size_t response_len, char *key)
{
    jsmntok_t *tokens = NULL;
    const jsmntok_t *t = NULL;
    char *current_token = NULL;
    jsmn_parser parser;
    int tokens_size = 50;
    size_t size;
    int ret;
    int i = 0;
    int len;
    flb_sds_t error_type = NULL;

    jsmn_init(&parser);

    size = sizeof(jsmntok_t) * tokens_size;
    tokens = flb_calloc(1, size);
    if (!tokens) {
        flb_errno();
        return NULL;
    }

    ret = jsmn_parse(&parser, response, response_len,
                     tokens, tokens_size);

    if (ret == JSMN_ERROR_INVAL || ret == JSMN_ERROR_PART) {
        flb_free(tokens);
        flb_debug("[aws_client] Unable to parse API response- response is not"
                  "not valid JSON.");
        return NULL;
    }

    /* return value is number of tokens parsed */
    tokens_size = ret;

    /*
     * jsmn will create an array of tokens like:
     * key, value, key, value
     */
    while (i < (tokens_size - 1)) {
        t = &tokens[i];

        if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)) {
            break;
        }

        if (t->type == JSMN_STRING) {
            current_token = &response[t->start];

            if (strncmp(current_token, key, strlen(key)) == 0) {
                i++;
                t = &tokens[i];
                current_token = &response[t->start];
                len = t->end - t->start;
                error_type = flb_sds_create_len(current_token, len);
                if (!error_type) {
                    flb_errno();
                    flb_free(tokens);
                    return NULL;
                }
                break;
            }
        }

        i++;
    }
    flb_free(tokens);
    return error_type;
}
/* gets the value of a key in a json string */
gets the value of a key in a json string
gets the value of a key in a json string
flb_sds_t flb_json_get_val(char *response, size_t response_len, char *key)
{
    jsmntok_t *tokens = NULL;
    const jsmntok_t *t = NULL;
    char *current_token = NULL;
    jsmn_parser parser;
    int tokens_size = 50;
    size_t size;
    int ret;
    int i = 0;
    int len;
    flb_sds_t error_type = NULL;
    jsmn_init(&parser);
    size = sizeof(jsmntok_t) * tokens_size;
    tokens = flb_calloc(1, size);
    if (!tokens) {
        flb_errno();
        return NULL;
    }
    ret = jsmn_parse(&parser, response, response_len,
                     tokens, tokens_size);
    if (ret == JSMN_ERROR_INVAL || ret == JSMN_ERROR_PART) {
        flb_free(tokens);
        flb_debug("[aws_client] Unable to parse API response- response is not"
                  "not valid JSON.");
        return NULL;
    }
    tokens_size = ret;
    while (i < (tokens_size - 1)) {
        t = &tokens[i];
        if (t->start == -1 || t->end == -1 || (t->start == 0 && t->end == 0)) {
            break;
        }
        if (t->type == JSMN_STRING) {
            current_token = &response[t->start];
            if (strncmp(current_token, key, strlen(key)) == 0) {
                i++;
                t = &tokens[i];
                current_token = &response[t->start];
                len = t->end - t->start;
                error_type = flb_sds_create_len(current_token, len);
                if (!error_type) {
                    flb_errno();
                    flb_free(tokens);
                    return NULL;
                }
                break;
            }
        }
        i++;
    }
    flb_free(tokens);
    return error_type;
}
flb_sds_t flb_json_get_val ( char * response , size_t response_len , char * key ) { jsmntok_t * tokens = NULL ; const jsmntok_t * t = NULL ; char * current_token = NULL ; jsmn_parser parser ; int tokens_size = 50 ; size_t size ; int ret ; int i = 0 ; int len ; flb_sds_t error_type = NULL ; jsmn_init ( & parser ) ; size = sizeof ( jsmntok_t ) * tokens_size ; tokens = flb_calloc ( 1 , size ) ; if ( ! tokens ) { flb_errno ( ) ; return NULL ; } ret = jsmn_parse ( & parser , response , response_len , tokens , tokens_size ) ; if ( ret == JSMN_ERROR_INVAL || ret == JSMN_ERROR_PART ) { flb_free ( tokens ) ; flb_debug ( " " " " ) ; return NULL ; } tokens_size = ret ; while ( i < ( tokens_size - 1 ) ) { t = & tokens [ i ] ; if ( t -> start == -1 || t -> end == -1 || ( t -> start == 0 && t -> end == 0 ) ) { break ; } if ( t -> type == JSMN_STRING ) { current_token = & response [ t -> start ] ; if ( strncmp ( current_token , key , strlen ( key ) ) == 0 ) { i ++ ; t = & tokens [ i ] ; current_token = & response [ t -> start ] ; len = t -> end - t -> start ; error_type = flb_sds_create_len ( current_token , len ) ; if ( ! error_type ) { flb_errno ( ) ; flb_free ( tokens ) ; return NULL ; } break ; } } i ++ ; } flb_free ( tokens ) ; return error_type ; }
gets the value of a key in a json string
gets the value of a key in a json string
/* return value is number of tokens parsed */ /*
     * jsmn will create an array of tokens like:
     * key, value, key, value
     */