A simple and complete json parser

This parser makes use of all the functions which reads the value of a json object. This parser is complete in all respects. You can make use of the functions to create parser for your various requirements


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

/*printing the value corresponding to boolean, double, integer and strings*/
void print_json_value(json_object *jobj){
  enum json_type type;
  printf("type: ",type);
  type = json_object_get_type(jobj); /*Getting the type of the json object*/
  switch (type) {
    case json_type_boolean: printf("json_type_booleann");
                         printf("value: %sn", json_object_get_boolean(jobj)? "true": "false");
                         break;
    case json_type_double: printf("json_type_doublen");
                        printf("          value: %lfn", json_object_get_double(jobj));
                         break;
    case json_type_int: printf("json_type_intn");
                        printf("          value: %dn", json_object_get_int(jobj));
                         break;
    case json_type_string: printf("json_type_stringn");
                         printf("          value: %sn", json_object_get_string(jobj));
                         break;
  }

}

void json_parse_array( json_object *jobj, char *key) {
  void json_parse(json_object * jobj); /*Forward Declaration*/
  enum json_type type;

  json_object *jarray = jobj; /*Simply get the array*/
  if(key) {
    jarray = json_object_object_get(jobj, key); /*Getting the array if it is a key value pair*/
  }

  int arraylen = json_object_array_length(jarray); /*Getting the length of the array*/
  printf("Array Length: %dn",arraylen);
  int i;
  json_object * jvalue;

  for (i=0; i< arraylen; i++){
    jvalue = json_object_array_get_idx(jarray, i); /*Getting the array element at position i*/
    type = json_object_get_type(jvalue);
    if (type == json_type_array) {
      json_parse_array(jvalue, NULL);
    }
    else if (type != json_type_object) {
      printf("value[%d]: ",i);
      print_json_value(jvalue);
    }
    else {
      json_parse(jvalue);
    }
  }
}

/*Parsing the json object*/
void json_parse(json_object * jobj) {
  enum json_type type;
  json_object_object_foreach(jobj, key, val) { /*Passing through every array element*/
    printf("type: ",type);
    type = json_object_get_type(val);
    switch (type) {
      case json_type_boolean: 
      case json_type_double: 
      case json_type_int: 
      case json_type_string: print_json_value(val);
                           break; 
      case json_type_object: printf("json_type_objectn");
                           jobj = json_object_object_get(jobj, key);
                           json_parse(jobj); 
                           break;
      case json_type_array: printf("type: json_type_array, ");
                          json_parse_array(jobj, key);
                          break;
    }
  }
} 

int main() {
  char * string = "{"sitename" : "joys of programming",
                     "categories" : [ "c" , ["c++" , "c" ], "java", "PHP" ],
                     "author-details": { "admin": false, "name" : "Joys of Programming", "Number of Posts" : 10 } 
                     }";
  printf("JSON string: %sn", string);
  json_object * jobj = json_tokener_parse(string);     
  json_parse(jobj);
}

The output of the program is something like this

JSON string: {"sitename" : "joys of programming",                     "categories" : [ "c" , ["c++" , "c" ], "java", "PHP" ],                     "author-details": { "admin": false, "name" : "Joys of Programming", "Number of Posts" : 10 }                      }
type: type: json_type_string
          value: joys of programming
type: type: json_type_array, Array Length: 4
value[0]: type: json_type_string
          value: c
Array Length: 2
value[0]: type: json_type_string
          value: c++
value[1]: type: json_type_string
          value: c
value[2]: type: json_type_string
          value: java
value[3]: type: json_type_string
          value: PHP
type: json_type_object
type: type: json_type_boolean
value: false
type: type: json_type_string
          value: Joys of Programming
type: type: json_type_int
          value: 10

json_object_array_put_idx- Add json object to Array at specified index

json_object_array_add is used to add a json object to an array. Using json_object_array_put_idx, we can put a json object at a specified index

The syntax of the function is

int json_object_array_put_idx (struct json_object *this, int idx, struct json_object *val)

Specify the object where the json object has to be added, index and the value (json object) as the arguments

When you specify objects at some particular index, you may miss (intentionally) objects at some indices. The values at those indices will be null

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json array*/
  json_object *jarray = json_object_new_array();

  /*Creating json strings*/
  json_object *jstring[3];
  jstring[0] = json_object_new_string("c");
  jstring[1] = json_object_new_string("c++");
  jstring[2] = json_object_new_string("php");

  /*Adding the above created json strings to the array*/
  int i;
  for (i=0;i<3; i++) { 
    json_object_array_put_idx(jarray,i+2, jstring[i]);
  }

  /*Form the json object*/
  json_object_object_add(jobj,"Categories", jarray);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Categories": [ null, null, "c", "c++", "php" ] }

json_object_array_add- Add json object to Array

json_object_array is used to add a json object to an array

The syntax of the function is

int 	json_object_array_add (struct json_object *this, struct json_object *val)

Specify the object where the json object has to be added, key and the value (json object) as the arguments

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json array*/
  json_object *jarray = json_object_new_array();

  /*Creating json strings*/
  json_object *jstring1 = json_object_new_string("c");
  json_object *jstring2 = json_object_new_string("c++");
  json_object *jstring3 = json_object_new_string("php");

  /*Adding the above created json strings to the array*/
  json_object_array_add(jarray,jstring1);
  json_object_array_add(jarray,jstring2);
  json_object_array_add(jarray,jstring3);

  /*Form the json object*/
  json_object_object_add(jobj,"Categories", jarray);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Categories": [ "c", "c++", "php" ] }

json_object_new_array- Create a new array json object

json_object_new_array is used to create a new array json object.

The syntax of the function is

json_object * json_object_new_array (char *s)

Specify the array as the argument and it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json array*/
  json_object *jarray = json_object_new_array();

  /*Creating json strings*/
  json_object *jstring1 = json_object_new_string("c");
  json_object *jstring2 = json_object_new_string("c++");
  json_object *jstring3 = json_object_new_string("php");

  /*Adding the above created json strings to the array*/
  json_object_array_add(jarray,jstring1);
  json_object_array_add(jarray,jstring2);
  json_object_array_add(jarray,jstring3);

  /*Form the json object*/
  json_object_object_add(jobj,"Categories", jarray);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Categories": [ "c", "c++", "php" ] }

json_object_new_double- Create a new double json object

json_object_new_double is used to create a new double json object.

The syntax of the function is

json_object * json_object_new_double (char *s)

Specify the double as the argument and it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json double*/
  json_object *jdouble = json_object_new_double(3.14);


  /*Form the json object*/
  json_object_object_add(jobj,"PI", jdouble);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "PI": 3.140000 }

json_object_new_string- Create a new string json object

json_object_new_string is used to create a new string json object.

The syntax of the function is

json_object * json_object_new_string (char *s)

Specify the string as the argument and it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json string*/
  json_object *jstring = json_object_new_string("Joys of Programming");


  /*Form the json object*/
  json_object_object_add(jobj,"Site Name", jstring);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Site Name": "Joys of Programming" }

json_object_new_boolean- Create a new boolean json object

json_object_new_boolean is used to create a new boolean json object.

The syntax of the function is

json_object * json_object_new_boolean (boolean b)

A non-zero value can be considered as boolean true

As you can see, it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json boolean*/
  json_object *jint = json_object_new_boolean(10);


  /*Form the json object*/
  json_object_object_add(jobj,"Technical Blog", jint);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Technical Blog": true }

json_object_new_int- Create a new Integer json object

json_object_new_int is used to create a new integer json object.

The syntax of the function is

json_object * json_object_new_int ()

As you can see, it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json integer*/
  json_object *jint = json_object_new_int(10);


  /*Form the json object*/
  json_object_object_add(jobj,"Number of posts", jint);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Number of posts": 10 }

json_object_new_object: Creating a new Json object

json_object_new_object is used to create a new json object. Once you create a json object, you can add more objects to it.

The syntax of the function is

json_object * json_object_new_object ()

As you can see, it returns a pointer to the json object

The following program shows the usage of the function


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

int main() {
  /*Creating a json object*/
  json_object * jobj = json_object_new_object();

  /*Creating a json string*/
  json_object *jstring = json_object_new_string("Joys of Programming");

  /*Creating a json integer*/
  json_object *jint = json_object_new_int(10);

  /*Creating a json boolean*/
  json_object *jboolean = json_object_new_boolean(1);

  /*Creating a json double*/
  json_object *jdouble = json_object_new_double(2.14);

  /*Creating a json array*/
  json_object *jarray = json_object_new_array();

  /*Creating json strings*/
  json_object *jstring1 = json_object_new_string("c");
  json_object *jstring2 = json_object_new_string("c++");
  json_object *jstring3 = json_object_new_string("php");

  /*Adding the above created json strings to the array*/
  json_object_array_add(jarray,jstring1);
  json_object_array_add(jarray,jstring2);
  json_object_array_add(jarray,jstring3);

  /*Form the json object*/
  /*Each of these is like a key value pair*/
  json_object_object_add(jobj,"Site Name", jstring);
  json_object_object_add(jobj,"Technical blog", jboolean);
  json_object_object_add(jobj,"Average posts per day", jdouble);
  json_object_object_add(jobj,"Number of posts", jint);
  json_object_object_add(jobj,"Categories", jarray);

  /*Now printing the json object*/
  printf ("The json object created: %sn",json_object_to_json_string(jobj));

}

The output of the program is something like this

The json object created: { "Site Name": "Joys of Programming", "Technical blog": true, "Average posts per day": 2.140000, "Number of posts": 10, "Categories": [ "c", "c++", "php" ] }

The output is something like this

{
    "Site Name": "Joys of Programming",
    "Technical blog": true,
    "Average posts per day": 2.140000,
    "Number of posts": 10,
    "Categories": [
        "c",
        "c++",
        "php" 
    ] 
}

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
    }
}