Parsing JSON file with json-c library segfault

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
User avatar
PsySc0rpi0n
Posts: 262
Joined: 2012-10-24 13:54
Location: Portugal

Parsing JSON file with json-c library segfault

#1 Post by PsySc0rpi0n »

Hello.

I have cloned and compiled json-c library from github and tried to start parsing a json file but I'm stuck at the very beginning when I started experimenting some examples and I can't (pretty much) follow the docs.

I have a json file with the following format:

Code: Select all

{
   "itemA":[
      {   
         "key1": "value1",
         "key2": "value2"
      },  
      {   
         "key1": "value3",
         "key3": "value4",
         "key4": "value5"
      },  
      {   
         "key2": "value6",
         "key3": "value7"
      },  
      {   
         "key1": "value8",
         "key2": "value9",
         "key3": "value10",
         "key4": "value11",
         "key5": "value12"
      }   
   ]   
}
The C code I have is the following:

Code: Select all

#include <stdio.h>
>>#include <json-c/json.h>
  #include <stdlib.h>
  
  #define JSONFILE "test.json"
  #define BUFFS 412
  
  int main(int argc, char** argv){
     FILE *fp;
     char buffer[BUFFS];
     json_object* parsed_obj;
     json_object* forwards;
     int n_items;
  
     if(!(fp = fopen(JSONFILE, "r"))){
        printf("Error opening file %s\n", JSONFILE);
        exit(-1);
     }   
     fread(buffer, BUFFS, 1, fp);
     fclose(fp);
  
     parsed_obj = json_tokener_parse(buffer);
     json_object_object_get_ex(parsed_obj, "forwards", &forwards);
     n_items = json_object_array_length(forwards);
     printf("Found %d forwards\n", json_object_get_int(n_items));
     return 0;
  }
Compiling this code, complains about the printf line. It says:
passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
I also tried to use:

Code: Select all

     size_t n_items;
     ...

    printf("Found %zu forwards\n", json_object_get_int(n_items));
but the error just changes to:
format '%zu' expects argument of type 'size_t', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
I also tried, the following, and the errors are in the comments

Code: Select all

     int32_t n_items;
     ...

    printf("Found %lu forwards\n", json_object_get_int(n_items));
    //format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
    printf("Found %zu forwards\n", json_object_get_int(n_items));
    //format '%zu' expects argument of type 'size_t', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
    printf("Found %u forwards\n", json_object_get_int(n_items));
    //passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
    printf("Found %d forwards\n", json_object_get_int(n_items));
    //passing argument 1 of 'json_object_get_int' makes pointer from integer without a cast [-Wint-conversion]
    printf("Found %ld forwards\n", json_object_get_int(n_items));
    //format '%ld' expects argument of type 'long int', but argument 2 has type 'int32_t' {aka 'int'} [-Wformat=]
So, it seems that no matter what I use, I can't make it work. It segfaults if I ignore the warning and run the program.
Where am I going wrong?

PS:
In json-c library github page, there is a link for some tutorials here:
https://github.com/json-c/json-c/wiki/L ... -tutorials

I followed this one to start understanding how to use the library:
https://progur.com/2018/12/how-to-parse-json-in-c.html

I use the code for arrays inside the json as the tutorial suggests, but it doesn't work for me!

User avatar
PsySc0rpi0n
Posts: 262
Joined: 2012-10-24 13:54
Location: Portugal

Re: Parsing JSON file with json-c library segfault

#2 Post by PsySc0rpi0n »

This error is fixed. I was mistakenly double using the same function to get the int value from the JSON array.

Post Reply