1

I have created the jobj by using json-c library successfully with the json_object_to_json_string(jobj) where jobj hold the data which is in json format.

I am able to print jobj on the console by using printf: printf ("%s",json_object_to_json_string(jobj));

Now I need write the jobj data to file in "C" language as jobj we declared as json_object * jobj = json_object_new_object();

Request to provide the info on the above i.e. writing the jobj to a file (fwrite).

Below is the example code snipet https://linuxprograms.wordpress.com/2010/08/19/json_object_new_array/

Below is the code snipet

static void prepared_line_file(char* line)
{
FILE* fptr;
fptr =  fopen("/home/ubuntu/Desktop/sample.json", "a")
fwrite(line,sizeof(char),strlen(line),logfile);
}

main()
{
json_object_object_add(jobj,"USER", jarray);
prepared_line_file(jobj);
}

am i missing anything?

  • 2
    ***Request to provide the info on the above*** Didn't you check the library documentation for this? Doesn't it mention anything about this? – kiner_shah Jan 20 '22 at 06:01
  • You know how to print something to standard output but not to a file? Brush up on stdio functions. – Shawn Jan 20 '22 at 06:17
  • yeah i am able to print on the console i.e, standard output by using printf function. here json_object *jobj where jobj is structure pointer for writing to file it expects const char* here i am not aware hoe to do please help me on this. can you please provide the references for the same – Manukumar Puttaswamy Jan 20 '22 at 06:24
  • i have followed the link https://linuxprograms.wordpress.com/2010/08/19/json_object_new_array/ – Manukumar Puttaswamy Jan 20 '22 at 06:31
  • 1
    What about using `fprintf` instead of `printf` ? – Joël Hecht Jan 20 '22 at 06:37
  • @JoëlHecht when i fopen("/home/ubuntu/Desktop/sample.json", "a") then its writing the data to file.. i missed here. i will let you know if i have further problem :) – Manukumar Puttaswamy Jan 20 '22 at 07:06
  • @JoëlHecht static void prepared_line_file(char* line) { FILE* fptr; fptr = fopen("/home/ubuntu/Desktop/sample.json", "a") fwrite(line,sizeof(char),strlen(line),logfile); } main() { json_object_object_add(jobj,"USER", jarray); prepared_line_file(jobj); } I was trying in the above method am i wrong or i missed any please let me know? – Manukumar Puttaswamy Jan 20 '22 at 07:16
  • @ManukumarPuttaswamy, please edit the post and include the code there. You shouldn't put code in comments. – kiner_shah Jan 20 '22 at 07:50
  • Welcome to StackOverflow! Please take the [tour] to learn how this site works. Specifically, don't add new information or refine the question in comments. Add this to your question by [edit]ing it. Make sure to _not delete_ the old contents, because this will render old comments useless. – the busybee Jan 20 '22 at 07:50
  • @kiner_shah i have edited the content please have look. please JoëlHecht to notify the changes – Manukumar Puttaswamy Jan 20 '22 at 08:15
  • In the example you gave, the function `json_object_to_json_string(jobj)` transforms the json object into a string (this is called serialization). For the content of the file to be readable, you should write this string in the file, not directly the json object. – Joël Hecht Jan 20 '22 at 09:10
  • can you please provide a prtotype or example for this to write this string in the file ? it will be helpful – Manukumar Puttaswamy Jan 20 '22 at 11:05
  • hi @JoëlHecht, I can 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":"logviewer","version":1.0, "logs":[{"loglevel":"INFO", "msg":"Info about car", "timestamp":"actual system time"}]} can you please suggest me how to approach? – Manukumar Puttaswamy Jan 22 '22 at 12:06
  • hi @JoëlHecht, I have raised 1 more query please provide support the link is https://stackoverflow.com/questions/70812826/request-to-help-guide-on-constructing-json-object – Manukumar Puttaswamy Jan 22 '22 at 12:31
  • hi @JoëlHecht, i need to replace "]}" string into "," in file. i am not getting how to do.. logic can you please help me here.. actually i am writing json data into a file there is a requirement to replace the "]}" string into "," pleas help me – Manukumar Puttaswamy Jan 24 '22 at 12:58

1 Answers1

0

I slightly modified the code you gave in order to write a readable string representing the json object to the file.

static void prepared_line_file(char* line)
{
    FILE* fptr;
    fptr = fopen("/home/ubuntu/Desktop/sample.json", "a");
    fwrite(line,sizeof(char),strlen(line),fptr);

    // fprintf(fptr, "%s", line); // would work too

    // The file should be closed when everything is written
    fclose(fptr);
}

main()
{
    // Will hold the string representation of the json object
    char *serialized_json;

    // Create an empty object : {}
    json_object * jobj = json_object_new_object();
    // Create an empty array : []
    json_object *jarray = json_object_new_array();

    // Put the array into the object : { "USER" : [] }
    json_object_object_add(jobj,"USER", jarray);

    // Transforms the binary representation into a string representation
    serialized_json = json_object_to_json_string(jobj);

    prepared_line_file(serialized_json);

    // Reports to the caller that everything was OK
    return EXIT_SUCCESS;
}
Joël Hecht
  • 1,766
  • 1
  • 17
  • 18