json object to be created in run time

199 views
Skip to first unread message

Deepankar Gupta

unread,
Apr 10, 2013, 8:07:18 AM4/10/13
to jansso...@googlegroups.com
Hi
Please share the sample code to use {fmt} in json library. 

the number of mac address and  ip address given to me will be known during run time. I have to construct the json object during runtime.

I want to create json something like below.  The number of pids will be known during run time.


{
    "meta" : {},
    "data" : {
    "map-vtag" : "1234567890",
    "map" : {
        "pid1" : {
            "mac" : [ "00:00:00:00:00:01" ],
            "ipv4": [ "10.0.1.3/24" ]
            },
        "pid2" : {
            "mac" : [ "00:00:00:00:00:02" ]
            },
        "pid3" : {
            "mac" : [ "00:00:00:00:00:03" ]
            },
        "pid4" : {
            "mac" : [ "00:00:00:00:00:04" ]
            },
        "pid5" : {
            "mac" : [ "00:00:00:00:00:05" ]
            }
        }
    }

Thanks in Advance

Regards
Deepankar Gupta

rogerz

unread,
Apr 10, 2013, 8:16:25 AM4/10/13
to jansso...@googlegroups.com
Use json_object_set() to insert new keys. 

I suggest you read the document on jansson site first. It is comprehensive and including many example.
--
--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users
---
You received this message because you are subscribed to the Google Groups "Jansson users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jansson-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
rogerz

Deepankar Gupta

unread,
Apr 12, 2013, 3:40:33 AM4/12/13
to jansso...@googlegroups.com
This is what i did to create array

fpJson = fopen("nMap.txt","w");
json_t *array = json_array();

 for(i=0;i<9;i++)
 {

    sprintf(index,"PID%d",i+1);
    if(strcmp(topo[i].ipAddress,"")!=0)
    {
         json =json_pack("{s : {s: [s], s: [s]}, }",
                index,
                    "mac", topo[i].macAddress,
                    "ipv4", topo[i].ipAddress
               );
    }
    else
    {

        json =json_pack("{s : {s: [s]}, }",
                index,
                    "mac", topo[i].macAddress
      );

    }
    json_array_append(array,json);
  }
  printf("json array size %d : \n",json_array_size(array));    // prints correct size of 9

//>> Now I want array to be packed

 char *str = "{s: {}, s: {s: s, s: o}}";

 jSon =json_pack(
        str,
        "meta",
        "data",
            "map-vtag", "123456",
            "map",
             json_array_get(array,0),
          );
 json_dumpf(jSon, fpJson,1);
 json_decref(jSon);


I am only able to get single only element from array packed, but not the whole array.  how should I do it ?



Regards
Deepankar
To unsubscribe from this group and stop receiving emails from it, send an email to jansson-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
rogerz

Petri Lehtinen

unread,
Apr 12, 2013, 4:11:25 AM4/12/13
to jansso...@googlegroups.com
Deepankar Gupta wrote:
> This is what i did to create array
>
> fpJson = fopen("nMap.txt","w");
> json_t *array = json_array();
>
> for(i=0;i<9;i++)
> {
>
> sprintf(index,"PID%d",i+1);
> if(strcmp(topo[i].ipAddress,"")!=0)
> {
> json =json_pack("{s : {s: [s], s: [s]}, }",
> index,
> "mac", topo[i].macAddress,
> "ipv4", topo[i].ipAddress
> );
> }
> else
> {
>
> json =json_pack("{s : {s: [s]}, }",
> index,
> "mac", topo[i].macAddress
> );
>
> }
> json_array_append(array,json);

You should use json_array_append_new() here to avoid a memory leak.
For details, see

http://www.digip.org/jansson/doc/2.4/apiref.html#reference-count

> }
> printf("json array size %d : \n",json_array_size(array)); // prints
> correct size of 9
>
> //>> Now I want array to be packed
>
> char *str = "{s: {}, s: {s: s, s: o}}";
>
> jSon =json_pack(
> str,
> "meta",
> "data",
> "map-vtag", "123456",
> "map",
> json_array_get(array,0),
> );
> json_dumpf(jSon, fpJson,1);
> json_decref(jSon);
>
>
> I am only able to get single only element from array packed, but not the whole
> array. how should I do it ?

Instead of calling json_array_get(), just pass the array itself. This
way it outputs the whole array and not just the first element.

Hope this helps.

Petri

Deepankar Gupta

unread,
Apr 12, 2013, 5:08:46 AM4/12/13
to jansso...@googlegroups.com
Petri
As suggested, it worked perfectly as required. I had tried passing "array" directly before, but was not working. Only thing different I did was to use, as suggested by you, "json_array_append_new" instead of "json_array_append".


Petri thanks for the help once again.

Regards
Deepankar
Reply all
Reply to author
Forward
0 new messages