#include #include #include #include typedef struct { char *token; int idx; } json_array_t; json_array_t tokenize_array(char *token) { json_array_t new; char *openB = strchr(token, '['); size_t cut = openB - token; new.token = malloc(cut); strncpy(new.token, token, cut); new.token[cut] = '\0'; openB++; char *endP; new.idx = strtol(openB, &endP, 10); if (openB == endP) new.idx = -1; return new; } json_object* __jc_array(json_object *obj, char *token) { json_array_t arry = tokenize_array(token); obj = json_object_object_get(obj, arry.token); if (json_object_is_type(obj, json_type_array)) obj = json_object_array_get_idx(obj, arry.idx); free(arry.token); return obj; } json_object* jq_clone(json_object *obj, const char* filter) { json_object *tmp = obj; char *tokcln = malloc((size_t)strlen(filter)); strcpy(tokcln, filter); const char d[] = "."; char *token = strtok(tokcln, d); while (token && tmp) { tmp = (token[strlen(token)-1] == ']') ? __jc_array(tmp, token) : json_object_object_get(tmp, token); token = strtok(NULL, d); } return tmp; }