json_object_is_type: Check the type of json object

json_object_is_type is used to check whether the type of the json object is same as that of the specified type. So you can easily guess that the function takes two arguments


#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);
    if (json_object_is_type(val, json_type_null)) {
     printf("json_type_nulln");
    }
    if (json_object_is_type(val, json_type_boolean)) {
     printf("json_type_booleann");
    }
    if (json_object_is_type(val, json_type_double)) {
     printf("json_type_doublen");
    }
    if (json_object_is_type(val, json_type_int)) {
     printf("json_type_intn");
    }
    if (json_object_is_type(val, json_type_object)) {
     printf("json_type_objectn");
    }
    if (json_object_is_type(val, json_type_array)) {
     printf("json_type_arrayn");
    }
    if (json_object_is_type(val, json_type_string)) {
     printf("json_type_stringn");
    }
  }
}

The output of the program is something like this

type: json_type_string
type: json_type_array
type: json_type_object

The input to the program is

{
    "sitename" : "joys of programming",
    "tags" : [
        "c" ,
        "c++",
        "java",
        "PHP"
    ],
    "author-details": {
        "name" : "Joys of Programming",
        "Number of Posts" : 10
    }
}
Advertisements

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_get_int : Get integer value of a json object

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


int json_object_get_int(struct json_object *);

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

The following program demonstrates this. the function json_parse accepts a json_object with one or more key:integer 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_int: printf("type: json_type_int, ");
 printf("value: %dn", json_object_get_int(val));
 break;
 }
 }
}
int main() {
 char * string = "{ "colors" : 7,
 "continents" : 7,
 "oceans" : 5
 }";
 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: { "colors" : 7,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "continents" : 7,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "oceans" : 5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
type: json_type_int, value: 7
type: json_type_int, value: 7
type: json_type_int, value: 5

As you can see our input to the program was


{
 "colors" : 7,
 "continents" : 7,
 "oceans" : 5
}

with json_object_get_int, we are able to get the integer values.