diff --git a/src/json-editor.c b/src/json-editor.c index 72b7489ccd41567c611ec1c81f8fecd4a85f1631..8b96889f929f4bbfab5c02d7612220bdc2484044 100644 --- a/src/json-editor.c +++ b/src/json-editor.c @@ -292,13 +292,34 @@ out: return 0; } +struct json_object *json_object_e_del_by_idx(struct json_object *arr, int idx) +{ + struct json_object *el, *new; + int i; + + if (!json_object_is_type(arr, json_type_array)) + return arr; + + new = json_object_new_array(); + + for (i = 0; i < json_object_array_length(arr); i++) { + if (i == idx) + continue; + + el = json_object_array_get_idx(arr, i); + json_object_array_add(new, el); + } + + return new; +} + /** * del a value key pair to src object * */ static int del_val(struct json_object *src, char *key) { - struct json_object *val; + struct json_object *val, *new, *new2; int idx[32]; int len, rv = -1; char parsed_key[32] = {0}; @@ -310,6 +331,9 @@ static int del_val(struct json_object *src, char *key) json_object_object_del(src, parsed_key); rv = 0; } else { + if (len > 1) + new = json_object_new_array(); + /* iterate every index that was appended to the key */ for (int i = 0; i < len; i++) { struct json_object *j_idx; @@ -328,14 +352,20 @@ static int del_val(struct json_object *src, char *key) /* at final index, delete at specified idx */ else { if (idx[i] > -1) - json_object_array_del_idx(val, idx[i], 1); + new2 = json_object_e_del_by_idx(val, idx[i]); else - json_object_array_del_idx(val, - json_object_array_length(val) - 1, 1); + new2 = json_object_e_del_by_idx(val, idx[i]); + + if (len > 1) + json_object_array_add(new, new2); + else + new = new2; rv = 0; } } + json_object_object_del(src, parsed_key); + json_object_object_add(src, parsed_key, new); } out: