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");
int main() {
 char * string = "{ "admin" : true,
 "reviewer" : false,
 "author" : true
 printf ("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s