2

Hello I have never used json-c before and im trying to Parse this json file:

{
"result": {
    "schedules": [
        {
            "message": "2 mn",
            "destination": "La Defense (Grande Arche)"
        },
        {
            "message": "6 mn",
            "destination": "La Defense (Grande Arche)"
        },
        {
            "message": "11 mn",
            "destination": "La Defense (Grande Arche)"
        },
        {
            "message": "15 mn",
            "destination": "La Defense (Grande Arche)"
        }
    ]
},
"_metadata": {
    "call": "GET /schedules/metros/1/berault/A",
    "date": "2021-12-19T12:36:19+01:00",
    "version": 4
}}

Im trying to get the content of the "message" keys into 4 different variables and print them as strings like this :

message = 2 min
message2 = 6 min
message3 = 11 min
message4 = 15 min

Here is my code :

#include <json-c/json.h>
#include <stdio.h>

int main(int argc, char **argv) {
  FILE *fp;
  char buffer[1024];
  struct json_object *parsed_json;
  struct json_object *message;
  struct json_object *message2;
  struct json_object *message3;
  struct json_object *message4;

  fp = fopen("test.json","r");
  fread(buffer, 2048, 1, fp);
  fclose(fp);

  parsed_json = json_tokener_parse(buffer);

  json_object_object_get_ex(parsed_json, "message", &message);
  json_object_object_get_ex(parsed_json, "message", &message2);
  json_object_object_get_ex(parsed_json, "message", &message3);
  json_object_object_get_ex(parsed_json, "message", &message4);

  printf("Message: %s\n", json_object_get_string(message));
  printf("Message: %s\n", json_object_get_string(message2));
  printf("Message: %s\n", json_object_get_string(message3));
  printf("Message: %s\n", json_object_get_string(message4));


}

but since my json have not the same structure as the one in the tutorial it doesn't work and return me this :

Message: (null) 
Message: (null)
Message: (null)
Message: (null)

Process finished with exit code 0

Any advices highly appreciated !

LilDev
  • 21
  • 3
  • 1
    `char buffer[1024];` and `fread(buffer, 2048, 1, fp);` - Do you see the problem? Make that `size_t bytes_read = fread(buffer, 1, sizeof buffer - 1, fp); if(bytes_read>0) buffer[bytes_read ] = '\0';` – Ted Lyngmo Dec 19 '21 at 11:57
  • 2
    Different JSON structures, different keys and values to fetch in the code. You can't copy-paste code to handle a different JSON input. – Some programmer dude Dec 19 '21 at 11:57
  • LilDev: Did the answer help? If you want me to clarify anything, please just ask. – Ted Lyngmo Dec 20 '21 at 11:49

1 Answers1

0

The first issue is that you try to read 1 element with the size 2048 into a buffer with the size 1024. You should read elements of size 1 and not read more bytes than you have space for in the buffer.

The next issue is that you try to parse what you've read without null terminating what you've read. fread does not assume that you're working with strings and will not do that automatically.

The third issue is that you are not looking up the message array before trying to access the individual elements.

I don't know much about this library so I will leave this with memory leaks. You'll have to read the documentation to fix that. It will do the lookup properly though:

#include <stdio.h>
#include <json-c/json.h>

int main() {
    char buffer[1024];
    FILE *fp = fopen("test.json", "r");
    size_t bytes_read = fread(buffer, 1, sizeof buffer - 1, fp);
    fclose(fp);

    if(bytes_read > 0) {
        buffer[bytes_read] = '\0';

        json_object *parsed_json = json_tokener_parse(buffer);

        json_object *result, *schedules;
        if(json_object_object_get_ex(parsed_json, "result", &result)) {
            if(json_object_object_get_ex(result, "schedules", &schedules)) {
                array_list *sched_arr = json_object_get_array(schedules);
                printf("elements = %zu\n", sched_arr->length);
                for(size_t idx = 0; idx < sched_arr->length; ++idx) {
                    json_object *message = array_list_get_idx(sched_arr, idx);
                    printf("Message: %s\n", json_object_get_string(message));
                }
                // free the array.  Perhaps done like this but please read the
                // doc:
                sched_arr->free_fn(sched_arr->array);
            }
        }
        // free parsed_json, please read the doc
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108