json_object_get_array: Access an array JSON object

json_object_get_array() is used to access an array within a  json object. JSON contains key:value pairs, where value can be an array. The values within an array can also be an array, integer, boolean, double.

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

void json_parse(json_object * jobj) {
  enum json_type type;
  json_object_object_foreach(jobj, key, val) {
    type = json_object_get_type(val);
    switch (type) {
      case json_type_array: printf("type: json_type_array, ");
                          jobj = json_object_object_get(jobj, key);
                          int arraylen = json_object_array_length(jobj);
                          printf("Array Length: %dn",arraylen);
                          int i;
                          json_object * jvalue;
                          for (i=0; i< arraylen; i++){
                            jvalue = json_object_array_get_idx(jobj, i);
                            printf("value[%d]: %sn",i, json_object_get_string(jvalue));
                          }
                          break;
    }
  }
}
int main() {
  char * string = "{ "tags": [ "c++", "php", "java"] 
                     }";
  printf ("JSON string: %sn", string);
  json_object * jobj = json_tokener_parse(string);
  json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.
On executing the program, we get the following output

$ ./a.out
JSON string: { "tags": [ "c++", "php", "java"]                      }
type: json_type_array, Array Length: 3
value[0]: c++
value[1]: php
value[2]: java

The input to the program was

{
    "tags": [
        "c++",
        "php",
        "java"
    ]
}

json_object_object_get: Get a json object

json_object_object_get() is used to get the json object. JSON contains key:value pairs. The value can be an array, integer, boolean, double. It can also be a json object. That means to access the values within the json object, we must recursively go through json objects. The following program shows this

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

void json_parse(json_object * jobj) {
  enum json_type type;
  json_object_object_foreach(jobj, key, val) {
    printf("type: ",type);
    type = json_object_get_type(val);
    switch (type) {
      case json_type_null: printf("json_type_nulln");
                           break;
      case json_type_boolean: printf("json_type_booleann");
                           printf("          value: %dn", json_object_get_boolean(val));
                           break;
      case json_type_double: printf("json_type_doublen");
                          printf("          value: %lfn", json_object_get_double(val));
                           break;
      case json_type_int: printf("json_type_intn");
                          printf("          value: %dn", json_object_get_int(val));
                           break;
      case json_type_object: printf("json_type_objectn");
                           jobj = json_object_object_get(jobj, key);
                           json_parse(jobj);
                           break;
      case json_type_string: printf("json_type_stringn");
                           printf("          value: %sn", json_object_get_string(val));
                           break;
    }
  }
}
int main() {
  char * string = "{"sitename" : "joys of programming",
                     "author-details": { "admin": false, "name" : "Joys of Programming", "Number of Posts" : 10 } 
                     }";
  json_object * jobj = json_tokener_parse(string);
  json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.
On executing the program, we get the following output


$ ./a.out
JSON string: {"sitename" : "joys of programming",                     "author-details": { "admin": false, "name" : "Joys of Programming", "Number of Posts" : 10 }                      }
type: json_type_string
          value: joys of programming
type: json_type_object
type: json_type_boolean
          value: 0
type: json_type_string
          value: Joys of Programming
type: json_type_int
          value: 10

The input to the program was


{
    "sitename" : "joys of programming",
    "author-details": {
        "admin": false,
        "name" : "Joys of Programming",
        "Number of Posts" : 10 
    } 
}

As you can see that “author-details” is again a json object. See how the json_object_object_get() is recursively used to parse the json objects

json_object_get_double: Get double value of json object

json_object_get_double() is used to get the double value of json object. The function takes a json_object as input and returns a double value.

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

void json_parse(json_object * jobj) {
  enum json_type type;
  json_object_object_foreach(jobj, key, val) {
    type = json_object_get_type(val);
    switch (type) {
      case json_type_double: printf("type: json_type_double, ");
                          printf("value: %lfn", json_object_get_double(val));
                          break;
    }
  }
}
int main() {
  char * string = "{ "PI" : 3.14,
                     "random" : 7e5,
                     }";
  printf ("JSON string: %sn", string);
  json_object * jobj = json_tokener_parse(string);
  json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.
On executing the program, we get the following output


$ ./a.out
JSON string: { "PI" : 3.14,                     "random" : 7e5,                     }
type: json_type_double, value: 3.140000
type: json_type_double, value: 700000.000000

The input to the program was


{
 "PI" : 3.14,
 "random" : 7e5
}

json_object_get_string : Get string value of a json object

json_object_get_string() function is used to get the string value of a json object. The syntax


char* json_object_get_string(struct json_object *);

It takes json_object as a parameter and returns back a string.

The following program demonstrates this. the function json_parse accepts a json_object with one or more key:string pairs.

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

void json_parse(json_object * jobj) {
 enum json_type type;
 json_object_object_foreach(jobj, key, val) {
 type = json_object_get_type(val);
 switch (type) {
 case json_type_string: printf("type: json_type_string, ");
 printf("value: %sn", json_object_get_string(val));
 break;
 }
 }
}
int main() {
 char * string = "{ "sitename" : "Joys of Programming",
 "purpose" : "programming tips",
 "platform" : "wordpress"
 }";
 printf ("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);
 json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.

On executing the program, we get the following output


$ ./a.out
JSON string: { "sitename" : "Joys of Programming",                     "purpose" : "programming tips",                     "platform" : "wordpress"                     }
type: json_type_string, value: Joys of Programming
type: json_type_string, value: programming tips
type: json_type_string, value: wordpress


As you can see our input to the program was


{
 "sitename" : "Joys of Programming",
 "purpose" : "programming tips",
 "platform" : "wordpress"
}

with json_object_get_string, we are able to get the string values.

json_object_get_boolean : Get boolean value of a json object

json_object_get_boolean() function is used to get the boolean value of a json object. The syntax


boolean json_object_get_boolean(struct json_object *);

It takes json_object as a parameter and returns back an boolean.

The following program demonstrates this. the function json_parse accepts a json_object with one or more key:boolean pairs.


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

void json_parse(json_object * jobj) {
 enum json_type type;
 json_object_object_foreach(jobj, key, val) {
 type = json_object_get_type(val);
 switch (type) {
 case json_type_boolean: printf("type: json_type_boolean, ");
 printf("value: %sn", json_object_get_boolean(val)? "true": "false");
 break;
 }
 }
}
int main() {
 char * string = "{ "admin" : true,
 "reviewer" : false,
 "author" : true
 }";
 printf ("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);
 json_parse(jobj);
}

Let’s compile the program. If you fail any compilation issues, refer the post.

On executing the program, we get the following output


$ ./a.out
JSON string: { "admin" : true,                     "reviewer" : false,                     "author" : true                     }
type: json_type_boolean, value: true
type: json_type_boolean, value: false
type: json_type_boolean, value: true

As you can see our input to the program was


{
 "admin" : true,
 "reviewer" : false,
 "author" : true
}

with json_object_get_boolean, we are able to get the boolean values.

json_object_object_foreach : Browse through every json object

You can browse through every json object using the macro json_object_object_foreach

The syntax


json_object_object_foreach(obj,key,val)

where obj is the json object you want to parse, key and value correspond to key: value pairs. As mentioned before, json_object_object_foreach is a macro defined something like this


#define json_object_object_foreach(obj,key,val) 
char *key; struct json_object *val; 
for(struct lh_entry *entry = json_object_get_object(obj)->head; ({ if(entry) { key = (char*)entry->k; val = (struct json_object*)entry->v; } ; entry; }); entry = entry->next )

So key, val are not some variables, but you can choose any random strings to correspond to key and value

The following program depicts this


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

int main() {
 char * string = "{"sitename" : "joys of programming",
 "tags" : [ "c" , "c++", "java", "PHP" ],
 "author-details": { "name" : "Joys of Programming", "Number of Posts" : 10 } 
 }";
 json_object * jobj = json_tokener_parse(string);
 enum json_type type;
 json_object_object_foreach(jobj, key, val) {
 printf("type: ",type);
 type = json_object_get_type(val);
 switch (type) {
 case json_type_null: printf("json_type_nulln");
 break;
 case json_type_boolean: printf("json_type_booleann");
 break;
 case json_type_double: printf("json_type_doublen");
 break;
 case json_type_int: printf("json_type_intn");
 break;
 case json_type_object: printf("json_type_objectn");
 break;
 case json_type_array: printf("json_type_arrayn");
 break;
 case json_type_string: printf("json_type_stringn");
 break;
 }
 }
}

Now let’s compile the program


$ gcc json_object_object_foreach.c  -l json
json_object_object_foreach.c: In function ‘main’:
json_object_object_foreach.c:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
json_object_object_foreach.c:9: note: use option -std=c99 or -std=gnu99 to compile your code

Oh, there are some errors, as you can see. This can be resolved by the option -std=c99


gcc json_object_object_foreach.c  -ljson -std=c99

Let’s execute the program


$ ./a.out
type: json_type_string
type: json_type_array
type: json_type_object

This program is not generic in nature. It has not shown the details of “author-details“. But you can write a recursive function to go through all the elements using json_object_object_foreach.

json_object_get_type: Get the type of JSON object

A json object can have any of the following types

  1. null,
  2. boolean,
  3. double,
  4. int,
  5. object,
  6. array,
  7. string,

If you are working with JSON-C, you may need to know the type of every JSON object you are parsing. json_object_get_type() gives you the type of every object.

The syntax of json_object_get_type()

enum json_type json_object_get_type(struct json_object *this);

It accepts a json_object and returns the type of the json object. The values can be

json_type_boolean,
json_type_double,
json_type_int,
json_type_object,
json_type_array,
json_type_string,

The following program demonstrates this

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

int main() {
char * string = "{"name" : "programming"}";
json_object * jobj = json_tokener_parse(string);
enum json_type type = json_object_get_type(jobj);
printf("type: ",type);
switch (type) {
case json_type_null: printf("json_type_nulln");
break;
case json_type_boolean: printf("json_type_booleann");
break;
case json_type_double: printf("json_type_doublen");
break;
case json_type_int: printf("json_type_intn");
break;
case json_type_object: printf("json_type_objectn");
break;
case json_type_array: printf("json_type_arrayn");
break;
case json_type_string: printf("json_type_stringn");
break;
}
}

Let’s compile and execute the program.

$ ./a.out
type: json_type_object

Compiling a json-c program in Linux

If you have installed json-c in your machine, let’s experiment with a simple JSON program making use of JSON-C functions


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

int main() {
 char * string = "{"name" : "joys of programming"}";
 json_object * jobj = json_tokener_parse(string);
 enum json_type type = json_object_get_type(jobj);
 printf("type: ",type);
 switch (type) {
 case json_type_null: printf("json_type_nulln");
 break;
 case json_type_boolean: printf("json_type_booleann");
 break;
 case json_type_double: printf("json_type_doublen");
 break;
 case json_type_int: printf("json_type_intn");
 break;
 case json_type_object: printf("json_type_objectn");
 break;
 case json_type_array: printf("json_type_arrayn");
 break;
 case json_type_string: printf("json_type_stringn");
 break;
 }
}

Let’s name the above file as json_type.c. Let’s compile it


$ gcc json_type.c

/tmp/ccNekoK9.o: In function `main':
json_type.c:(.text+0x19): undefined reference to `json_tokener_parse'
json_type.c:(.text+0x29): undefined reference to `json_object_get_type'
collect2: ld returned 1 exit status

As you see, there are errors. To compile the program, we must use the “-l” option with the library name “json“. This helps in dynamic loading of the library libjson.


$ gcc json_type.c -l json

It’s successfully compiled. Now let’s execute the program


$ ./a.out
type: json_type_object

See the output of the program. You can learn more about working with json-c.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight data-interexchange format. Prior to JSON, XML was the most commonly used data inter-exchange format. XML still has its dominance. But JSON is also gaining up because of its simplicity. JSON represents the data as string:value pairs, where the value can be string, numbers, arrays, boolean values or even null value.

Take for example

1. string: number

{
 "rollnumber": 1
}

2. string: string

{
 "sitename": "programming"
}

3. string: boolean value

{
 "student" : true
}

4. string: array

[js]
{
"tags" : [
"cpp",
"c",
"PHP"
]
}
[/js]

5. string:object (Note that object is nothing but any of the four representations shown above)

[js]
{
"site": {
"sitename": "programming",
"tags": [
"cpp",
"c",
"PHP"
]
}
}
[/js]

You can learn more about these representations.

How to install json-glib in Linux (Ubuntu)?

json-glib is a C library for manipulating JSON data in C/C++– for serializing and deserializing JSON data. It can be downloaded from here. But if you are working in Ubuntu or other debian based systems you can execute the following commands


$ sudo apt-get install libjson-glib-1.0-0 libjson-glib-1.0-0-dev

If you want to debug your programs and see the various steps of serializing/deserializing you can also install the libjson-glib – debug symbols package


$ sudo apt-get install libjson-glib-1.0-0-dbg

For documentation related to json-glib, you must install the following package

$ sudo apt-get install libjson-glib-1.0-0-doc

This documentation will then be available in file:///usr/share/gtk-doc/html/json-glib/index.html