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");
    case json_type_double: printf("json_type_doublen");
                        printf("          value: %lfn", json_object_get_double(jobj));
    case json_type_int: printf("json_type_intn");
                        printf("          value: %dn", json_object_get_int(jobj));
    case json_type_string: printf("json_type_stringn");
                         printf("          value: %sn", json_object_get_string(jobj));


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);
    else {

/*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);
      case json_type_object: printf("json_type_objectn");
                           jobj = json_object_object_get(jobj, key);
      case json_type_array: printf("type: json_type_array, ");
                          json_parse_array(jobj, key);

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);     

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

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