Setting a struct from a json file

218 views
Skip to first unread message

Necdet Şanlı

unread,
Apr 11, 2023, 6:24:54 AM4/11/23
to json-c
Hi, I'm a json-c newbie, just started to using it and I want to set a struct by reading from a json file which has a similar format as in example. Can you please help me? I tried but couldn't manage to do it. I also looked up 

json file example and struct  that I want to set: 


{"connectedDevices": {
    "ethercat" : [
        {"master0" : [
            {"deviceName": "Name1", "alias": 0, "position": 0, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0},
            {"deviceName": "Name2", "alias": 0, "position": 1, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0},
            {"deviceName": "Name3", "alias": 0, "position": 2, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0},
            {"deviceName": "Name4", "alias": 0, "position": 3, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0},
            {"deviceName": "Name5", "alias": 0, "position": 4, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0}
        ]},
        {"master1" : [
            {"deviceName": "Name6", "alias": 0, "position": 0, "vendorID": 2, "productCode": 0, "revisionNo": 0, "serialNo": 0}
        ]},
        {"master2" : [
        ]}
    ]
}}    

struct Device {
    char protocol[32];
    char name[128];
    int masterIndex;
    int alias;
    int position;
    int vendorID;
    int productCode;
    int revisionNo;
    int serialNo;
}

Thanks in advance. 


Eric Hawicz

unread,
Apr 11, 2023, 6:18:00 PM4/11/23
to jso...@googlegroups.com
How are you trying?  It'll be easier to suggest fixes if you explain more about what kinds of errors/problems you're running into.

 Based on what you wrote, I expect you'd start with:
* json_object_from_file() to parse the file, then
* use json_object_object_get_ex() to walk down through the tree to the "ethercat" object
* json_object_array_length()/_get_idx() to iterate through the arrays, and
* json_object_get_string()/json_object_get_int() for the final field values

oh, and maybe json_object_object_foreach() if the "master0", "master1", etc...  object keys aren't fixed.

Alternately, I think someone might have built a more general "struct mapping" library on top of json-c, but if you're only doing this for a single struct it's probably easier just to hand code it.

Eric

Necdet Şanlı

unread,
Apr 12, 2023, 1:39:47 PM4/12/23
to jso...@googlegroups.com
Dear Eric Hawicz 

Thank you for your fast answer. In the code below I tried to access the master and print the information, if I succeed I would try to set the struct later but  I had this error :  jsontest: ./json_object.c:1484: json_object_array_length: Assertion `json_object_get_type(jso) == json_type_array' failed. 
Aborted. 
I think I dont use correct functions. 

Here is the code: 

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

typedef struct Device {
    char protocol[32];
    char name[128];
    int masterIndex;
    int alias;
    int position;
    int vendorID;
    int productCode;
    int revisionNo;
    int serialNo;
}Device;



int main(void)
{
    int ethercat_size;
    json_object *root, *temp;

    root = json_object_from_file("test.JSON");
    if (!root)
        return 1;
   
    json_object *connected_devices = json_object_object_get(root, "connectedDevices");
    printf("Connected devices are : %s\n", json_object_get_string(connected_devices));
   
    json_object *ethercat = json_object_object_get(root, "ethercat");
    ethercat_size = json_object_array_length(ethercat);

    for (int i = 0; i < ethercat_size; ++i) {
        temp = json_object_array_get_idx(ethercat, i);
        printf("\t Master: %s\n", json_object_get_string(temp));
        int master_size = json_object_array_length(temp);
        for (int k = 0; k < master_size; ++k) {
            json_object *temp2 = json_object_array_get_idx(temp, k);
            json_object *device_name = json_object_object_get(temp2, "deviceName");
            printf("\t deviceName: %s\n", json_object_get_string(device_name));
            json_object *alias = json_object_object_get(temp2, "alias");
            printf("\t alias: %d\n", json_object_get_int(alias));
            json_object *position = json_object_object_get(temp2, "position");
            printf("\t position: %d\n", json_object_get_int(position));
            json_object *vendorID = json_object_object_get(temp2, "vendorID");
            printf("\t vendorID: %d\n", json_object_get_int(vendorID));
            json_object *productCode = json_object_object_get(temp2, "productCode");
            printf("\t alias: %d\n", json_object_get_int(productCode));
            json_object *revisionNo = json_object_object_get(temp2, "revisionNo");
            printf("\t alias: %d\n", json_object_get_int(revisionNo));
            json_object *serialNo = json_object_object_get(temp2, "serialNo");
            printf("\t alias: %d\n", json_object_get_int(serialNo));
        }

    }
   
    json_object_put(root);

    return 0;
}

Best Regards
Necdet Şanlı


Eric Hawicz <haw...@gmail.com>, 12 Nis 2023 Çar, 01:18 tarihinde şunu yazdı:
--
You received this message because you are subscribed to a topic in the Google Groups "json-c" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-c/5aXd8aAcd9U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-c+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/json-c/CAMwP47Aw2QsyWaGKYYLRvAV6%2BgiMqoGmzUQuE5UerB%2BHNB%3DMFg%40mail.gmail.com.

Eric Hawicz

unread,
Apr 12, 2023, 5:58:24 PM4/12/23
to json-c
You're trying to get "ethercat" from root, but you want to grab it out of connected_devices instead.

Also, you want to use json_object_to_json_string instead of json_object_get_string for printing out an object.

Also, each entry in ethercat is an object, isn't it?

Eric

Necdet Şanlı

unread,
Apr 14, 2023, 4:12:48 PM4/14/23
to jso...@googlegroups.com
Dear Eric Hawicz 

Thank you for your guidance, I could manage to do it with your help..

Best Regards
Necdet Şanlı

Eric Hawicz <haw...@gmail.com>, 13 Nis 2023 Per, 00:58 tarihinde şunu yazdı:
--
You received this message because you are subscribed to a topic in the Google Groups "json-c" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/json-c/5aXd8aAcd9U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to json-c+un...@googlegroups.com.

Necdet Şanlı

unread,
Apr 24, 2023, 12:35:24 PM4/24/23
to jso...@googlegroups.com
Dear Erick Hawicz

This time I'm trying to create a json file from a C structure but I have a little problem with it. I want master0 and master1 to be included in the json file I created under ethercat, as in the first e-mail I sent, but I cannot do this with the json_object_array_add fucntion as can be seen in the code. Here is the code I wrote and the output: 

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

typedef struct Device {
    char *protocol;
    char *name;
    int masterIndex;
    int alias;
    int position;
    int vendorID;
    int productCode;
    int revisionNo;
    int serialNo;
}Device;

void sgets(char* p)
{
    int c;

    while ((c = getchar()) != '\n') {
        *p++ = (char)c;
    }

    *p = '\0';
}

Device* create_device()
{
    Device *dp = (Device*)malloc(sizeof(Device));
    static char device_name[256];

    dp->protocol = "ethercat";
   
    printf("Device name: ");
    sgets(device_name);
    dp->name = device_name;

    printf("masterIndex: ");
    scanf("%d", &dp->masterIndex);

    printf("alias: ");
    scanf("%d", &dp->alias);

    printf("position: ");
    scanf("%d", &dp->position);

    printf("vendorID: ");
    scanf("%d", &dp->vendorID);

    printf("productCode: ");
    scanf("%d", &dp->productCode);

    printf("revisionNo: ");
    scanf("%d", &dp->revisionNo);

    printf("serialNo: ");
    scanf("%d", &dp->serialNo);


   return dp;

}

int main(void)
{
    Device *dp = create_device();
    const char* filename = "test2.json";
    json_object *root = json_object_new_object();

    if (!root)
        return 1;

    json_object *connectedDevices = json_object_new_object();
    json_object_object_add(root, "connectedDevices", connectedDevices);

    printf("connected devices sorun yok\n");

    json_object *ethercat = json_object_new_array();
    json_object_object_add(connectedDevices, "ethercat", ethercat);
   
   
    printf("ethercat sorun yok\n");
   
    json_object *master0 = json_object_new_array();
    json_object_array_add(ethercat, master0);

    json_object *master1 = json_object_new_array();
    json_object_array_add(ethercat, master1);
   
     

    printf("master1 sorun yok\n");

    json_object *deviceObjects[6];

    for (int i = 0; i < 5; ++i) {
        deviceObjects[i] = json_object_new_object();
        json_object_object_add(deviceObjects[i], "deviceName", json_object_new_string(dp->name));
        json_object_object_add(deviceObjects[i], "alias", json_object_new_int(dp->alias));
        json_object_object_add(deviceObjects[i], "position", json_object_new_int(dp->position));
        json_object_object_add(deviceObjects[i], "vendorID", json_object_new_int(dp->vendorID));
        json_object_object_add(deviceObjects[i], "productCode", json_object_new_int(dp->productCode));
        json_object_object_add(deviceObjects[i], "revisionNo", json_object_new_int(dp->revisionNo));
        json_object_object_add(deviceObjects[i], "serialNo", json_object_new_int(dp->serialNo));
        json_object_array_add(master0, deviceObjects[i]);
    }

    deviceObjects[5] = json_object_new_object();
    json_object_object_add(deviceObjects[5], "deviceName", json_object_new_string(dp->name));
    json_object_object_add(deviceObjects[5], "alias", json_object_new_int(dp->alias));
    json_object_object_add(deviceObjects[5], "position", json_object_new_int(dp->position));
    json_object_object_add(deviceObjects[5], "vendorID", json_object_new_int(dp->vendorID));
    json_object_object_add(deviceObjects[5], "productCode", json_object_new_int(dp->productCode));
    json_object_object_add(deviceObjects[5], "revisionNo", json_object_new_int(dp->revisionNo));
    json_object_object_add(deviceObjects[5], "serialNo", json_object_new_int(dp->serialNo));
    json_object_array_add(master1, deviceObjects[5]);

    if (json_object_to_file(filename, root))
        printf("Error: failed to save %s!!\n", filename);
    else
        printf("%s saved\n", filename);

   
    json_object_put(root);

    return 0;    
}

    "connectedDevices": {
        "ethercat": [
            [
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                },
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                },
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                },
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                },
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                }
            ],
            [
                {
                    "deviceName": "necdet",
                    "alias": 0,
                    "position": 0,
                    "vendorID": 0,
                    "productCode": 0,
                    "revisionNo": 0,
                    "serialNo": 0
                }
            ]
        ]
    }
}

Thanks in advance.

Necdet Şanlı <nejo....@gmail.com>, 14 Nis 2023 Cum, 23:12 tarihinde şunu yazdı:

Necdet Şanlı

unread,
Apr 25, 2023, 2:31:34 PM4/25/23
to jso...@googlegroups.com
I made a change in the section below and got what I want. Please let me know if its not a good/efficient way. 

json_object *ethercat = json_object_new_array();
    json_object_object_add(connectedDevices, "ethercat", ethercat);

    json_object *ethercat_0 = json_object_new_object();
    json_object_array_add(ethercat, ethercat_0);

    json_object *ethercat_1 = json_object_new_object();
    json_object_array_add(ethercat, ethercat_1);
   
   
    json_object *master0 = json_object_new_array();
    json_object_object_add(ethercat_0, "master0", master0);

    json_object *master1 = json_object_new_array();
    json_object_object_add(ethercat_1, "master1", master1);

Necdet Şanlı <nejo....@gmail.com>, 24 Nis 2023 Pzt, 19:35 tarihinde şunu yazdı:

Eric Hawicz

unread,
Apr 26, 2023, 1:21:24 PM4/26/23
to jso...@googlegroups.com
On Tue, Apr 25, 2023 at 2:31 PM Necdet Şanlı <nejo....@gmail.com> wrote:
I made a change in the section below and got what I want. Please let me know if its not a good/efficient way. 

json_object *ethercat = json_object_new_array();
    json_object_object_add(connectedDevices, "ethercat", ethercat);

    json_object *ethercat_0 = json_object_new_object();
    json_object_array_add(ethercat, ethercat_0);

    json_object *ethercat_1 = json_object_new_object();
    json_object_array_add(ethercat, ethercat_1);
   
   
    json_object *master0 = json_object_new_array();
    json_object_object_add(ethercat_0, "master0", master0);

    json_object *master1 = json_object_new_array();
    json_object_object_add(ethercat_1, "master1", master1);

That looks entirely reasonable.  I'm glad you were able to get it working!

Eric
Reply all
Reply to author
Forward
0 new messages