0

let's say I have this json-array : "test": { "d": "med", "min": [ "low","med","high"]}

now using json-c api I want to remove the high string from the key min.

int arrLen=json_object_array_length(test_array_min);
    int j=0;
    json_object *high_ptr = NULL;
    for ( j = 0; j < arrLen; j++)
    {
        high_ptr = json_object_array_get_idx(test_array_min, j);
        clog_debug(NULL, "array[%d] : %s", j,json_object_to_json_string(high_ptr));
         if(!strcmp(json_object_get_string(high_ptr), "high")) {
            json_object_array_del_idx((test_array_min),(j),1);
            break;
        }
    }

but this fails, even though I can see clearly I am pointing to the right json field of high when I print it's string value. what am I doing wrong?

looking for json-c api, looking at examples how to remove element from json-c array.

Nizan
  • 1
  • 1
  • 1
    _How_ it fails? – Justinas Jul 28 '23 at 07:27
  • You are not showing the value of `test_array`. Is it really the array which is the value of the key `min` (`[ "low","med","high"]`) or is it the whole object `test`, as the name suggests? Please [edit] your question and make sure the code is a [mcve] – Fabio says Reinstate Monica Jul 28 '23 at 07:34
  • You are right, I ment that it hold the value of min array inside test. the name suggested otherwise. edited – Nizan Jul 28 '23 at 07:49
  • What does `json_object_array_del_idx` return? [The manual](https://json-c.github.io/json-c/json-c-0.16/doc/html/json__object_8h.html#a722eca9f578704d3af38b97549242c1f) says it returns "0 if the elements were successfully deleted". Have you checked it? – Fabio says Reinstate Monica Jul 28 '23 at 08:53

0 Answers0