How to remove an array element in json-c?

SCO

Assuming the following JSON object, an array of [epoch, value] arrays:

[ [0,1], [1912312314,2], [1912312857,5] ]

What's the correct way to remove an element of the array ? The idea is to remove the one with an epoch alder than a given value. I use json-c 0.11.

I tried :

   json_object *jsonHeatmapObj;
   jsonHeatmapObj = json_tokener_parse ( "[ [0,1], [1912312314,2], [1912312857,5] ]" );

    for ( int idx=0 ; idx < json_object_array_length(jsonHeatmapObj) ; idx++ ) {
       json_object *curJsonHeatpointObj = json_object_array_get_idx ( jsonHeatmapObj , idx );
       int x = json_object_get_int ( json_object_array_get_idx ( curJsonHeatpointObj , 0 ) );
       if ( x < time(NULL) - 10 ) {
          json_object_put ( curJsonHeatpointObj );
       }
       printf("\t[%d]=%s\n", idx, json_object_to_json_string(jsonHeatmapObj));
    }

This fails (SIGSEGV) when calling json_object_to_json_string() with the adjusted object.

Thank you

logc

I think there are two separate issues with that code: one is that it is using json_object_put as if json_object_array_get_idx had removed the element it returns from the original array (something that is not at all clear from the API documentation), and second that it uses time(NULL) as if it was returning an integer, but it is returning a time_t struct.

The first one can be solved by creating a new JSON array and only including items in it if the condition is met. Here I am avoiding the second issue by using a time that is set to the largest value in the array:

struct json_object *newHeatMap = json_object_new_array();
for ( int idx=0 ; idx < json_object_array_length(jsonHeatmapObj) ; idx++ ) {
    json_object *curJsonHeatpointObj = json_object_array_get_idx ( jsonHeatmapObj , idx );
    int x = json_object_get_int ( json_object_array_get_idx ( curJsonHeatpointObj , 0 ) );
    if ( x < 1912312857 ) {
        json_object_array_add(newHeatMap, curJsonHeatpointObj);
    }
    printf("\t[%d]=%s\n", idx, json_object_to_json_string(newHeatMap));
}

I get the following results, which I hope are what you intend to see:

$ gcc -I/opt/local/include -L/opt/local/lib/ -ljson-c main.c && ./a.out example.json
    [0]=[ [ 0, 1 ] ]
    [1]=[ [ 0, 1 ], [ 1912312314, 2 ] ]
    [2]=[ [ 0, 1 ], [ 1912312314, 2 ] ]

The second problem has already been answered for instance here on SO

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove an array element in json-c?

From Dev

How to remove json element?

From Dev

How to remove json element?

From Dev

Remove an element from Json array

From Dev

Remove JSON array element in scala

From Dev

How to remove parent element of each collection and represent it as array in json

From Dev

How to remove element in mongodb array

From Dev

How to remove the matching element of the array

From Dev

How to remove element of array in php?

From Dev

How to remove the first element in an array?

From Dev

How to remove whitespace in array element?

From Dev

How to remove element in mongodb array

From Dev

how to remove javascript array element?

From Dev

how to remove an array element in array of array in js?

From Dev

How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript

From Dev

Remove an element from an array of structures in C?

From Dev

How to remove the nth element from the end of an array

From Java

How to remove an element from an array in Swift

From Java

How to remove element from array in forEach loop?

From Dev

How to remove specific element from array with RactiveJs

From Dev

How to remove element from array if values matched?

From Dev

How to remove JavaScript array element and reset keys

From Dev

How to remove substring matching to any element of array

From Dev

How to remove an element from a String array?

From Dev

How to remove an element in jsonb integer array in PostgreSQL

From Dev

How to remove an element of array in foreach loop?

From Dev

How to remove an empty element in array php

From Dev

How to remove the blank element in javascript array?

From Dev

How to remove mutable array element from NSUserdefaults?

Related Related

  1. 1

    How to remove an array element in json-c?

  2. 2

    How to remove json element?

  3. 3

    How to remove json element?

  4. 4

    Remove an element from Json array

  5. 5

    Remove JSON array element in scala

  6. 6

    How to remove parent element of each collection and represent it as array in json

  7. 7

    How to remove element in mongodb array

  8. 8

    How to remove the matching element of the array

  9. 9

    How to remove element of array in php?

  10. 10

    How to remove the first element in an array?

  11. 11

    How to remove whitespace in array element?

  12. 12

    How to remove element in mongodb array

  13. 13

    how to remove javascript array element?

  14. 14

    how to remove an array element in array of array in js?

  15. 15

    How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript

  16. 16

    Remove an element from an array of structures in C?

  17. 17

    How to remove the nth element from the end of an array

  18. 18

    How to remove an element from an array in Swift

  19. 19

    How to remove element from array in forEach loop?

  20. 20

    How to remove specific element from array with RactiveJs

  21. 21

    How to remove element from array if values matched?

  22. 22

    How to remove JavaScript array element and reset keys

  23. 23

    How to remove substring matching to any element of array

  24. 24

    How to remove an element from a String array?

  25. 25

    How to remove an element in jsonb integer array in PostgreSQL

  26. 26

    How to remove an element of array in foreach loop?

  27. 27

    How to remove an empty element in array php

  28. 28

    How to remove the blank element in javascript array?

  29. 29

    How to remove mutable array element from NSUserdefaults?

HotTag

Archive