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));
int main() {
  char * string = "{ "PI" : 3.14,
                     "random" : 7e5,
  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: { "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

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