0

I am new to json-c.

I am able to create JSON object in the format:

{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}

but I need help in creating the JSON object in the format:

{"module":"log","version":1.0, "logs":[{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}]}

Code is below:

/*Line is the messge of string which is passed from other module*/
static void json_file(char *line)
{
    FILE* lg;
    lg = fopen(JSON_FILE,"ab+");

    char *json = "{\"key1\":\"data\", \"key2\":1.0, \"Logs\":[";
    fwrite(line, sizeof(char), strlen(line),lg);
    fputs("]}", lg);
    /*replace last ']}' with ',' json string and ]} */
    char stringtofind[4] = "]}";
    char get_line[2000];
    fgets(get_line, sizeof(get_line), lg);
    if (strstr(get_line, stringtofind) != NULL)
    {
        printf("substring found \n");
        /* Help Required here*/
        /* please help */
        // Need to replace the  "]}" into ","
    }
}
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
  • Can you give more info on "how" you want to create the json object? programmatically in C? what did you try? can you publish a snippet of your code? (don't forget the backticks `\`\`\``) – vinalti Jan 22 '22 at 12:35
  • Did you check the documentation? I found some tutorial on internet : https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/new.md – vinalti Jan 22 '22 at 12:37
  • Thanks, @vinalti, i have constructed the json data with the link procvided by you. there is a requirement to replace the "]}" string into "," in json data which i have constrcuted i am not able to do that please help me – Manukumar Puttaswamy Jan 24 '22 at 13:00
  • Can you provide more details in your original publication about this aspect, give an example off what you currently get and what you should get. (use the edit button) Also provide the code you already have made – vinalti Jan 24 '22 at 13:09
  • @vinalti i have updated my query in code section please have a look – Manukumar Puttaswamy Jan 24 '22 at 13:13
  • I published an answer... – vinalti Jan 24 '22 at 14:15

1 Answers1

0

From what I understand you want to use json-c, right ? Then why not to use the real syntax ?

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




json_object *generate_log_object(char *log_level, char *msg, char *timestamp){
   // {}
   json_object *child = json_object_new_object();
   //  {"loglevel": ... }
   json_object_object_add(child, "loglevel", json_object_new_string(log_level));
   // {"loglevel":"...", "msg":"..."}
   json_object_object_add(child, "msg", json_object_new_string(msg));
   // {"loglevel":"...", "msg":"...", "timestamp":"..."}
   json_object_object_add(child, "timestamp", json_object_new_string(timestamp));
   return child
}



int main(void)
{
   json_object *root = json_object_new_object();
   if (!root)
      return 1;
   // {"module":"log"}
   json_object_object_add(root, "module", json_object_new_string("log"));

   // {"module":"log","version":1.0 }
   json_object_object_add(root, "version", json_object_new_string("1.0"));


   // {"module":"log","version":1.0, "logs":[]}
   json_object *logs = json_object_new_array();
   json_object_object_add(root, "logs", logs);


   // {"module":"log","version":1.0, "logs":[{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}]}
   char *log1 = generate_log_object("INFO", "Info about car", "system time");
   json_object_array_add(children, log1);
}

Just as a side note, I didn't use C for the last 5 years and I never used json-c. I didn't compile the code, so there may be some typos, or small errors I let you fix.

vinalti
  • 966
  • 5
  • 26