Error while Loading Json file

In my current directory I have a file named demo.json and I want to load this json file.

But that is not working, because something is wrong in the I think
json_parser_load_from_file(parser, "demo.json", &error);
Because I get the print statement of the if statement

My code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include <json-glib/json-glib.h>

struct Book {
    gint64      Sets;
    gint64      Tlower;
    char        *Cleaner;
    char        *Remark;
};

int main() {
    GError *error;

    if (argc < 2) {
        printf("Usage: test <filename.json>\n");
        return EXIT_FAILURE;
    }

    JsonParser *parser = json_parser_new();
    json_parser_load_from_file(parser, "demo.json", &error);
    if(error) {
        printf("Unable to parse `%s': %s\n", argv[1], error->message);
        g_error_free(error);
        g_object_unref(parser);
        return EXIT_FAILURE;
    }

    JsonNode *root = json_parser_get_root(parser);

    JsonObject *stuff = json_node_get_object(root);

    
    struct Book Book1 = {
        .Sets = json_object_get_int_member(stuff, "Sets"),
        .Tlower = json_object_get_int_member(stuff, "Tlower"),
        .Cleaner = strdup(json_object_get_string_member(stuff, "Cleaner")),
        .Remark = strdup(json_object_get_string_member(stuff, "Remark"))
    };


    printf(
        "Sets = %ld, Tlower = %ld, Cleaner = '%s', Remark = '%s'\n",
        Book1.Sets, Book1.Tlower, Book1.Cleaner, Book1.Remark
    );

    g_object_unref(parser);
    json_object_unref(root);

    return EXIT_SUCCESS;
}
Last edited on
I have a folder and from that folder I want to load a file that is named demo.json
What folder? You do not specify one. This way the data is loaded from the current directory which might be the execute path or another determined by ide.
In my current directory I have a file named demo.json and I want to load this json file.
In my current directory

What is your "current directory"?

What directory is your program running in?

Your program is an exe file. That exe file is in a directory. Is it the same directory as demo.json ?
I would think that on line 22 it should be argv[1].

What is the error message?
@coder
indeed with argv[1] if I pass the arguments like
[Code]gcc test.c - o test demo.json[\Code]

The code is working, but I want to add the location of the file in test.c file how do I achieve this?
You can provide the path:

test my/path/to/demo.json
Topic archived. No new replies allowed.