Source Code File 10-03_pretty

10-03_pretty.c

#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>

int main()
{
    const char filename[] = "sample.json";
    json_object *jdata;
    const char *jstring;

    jdata = json_object_from_file(filename);
    if( jdata==NULL )
    {
        fprintf(stderr,"Unable to process %s\n",filename);
        exit(1);
    }

    jstring = json_object_to_json_string_ext(
            jdata,
            JSON_C_TO_STRING_PRETTY
            );
    puts(jstring);

    return(0);
}

Output

{
  "firstName":"Simon",
  "middleName":"Bar",
  "lastName":"Sinister",
  "address":{
    "street":"123 Evil Ave.",
    "city":"Bigtown",
    "state":"New York",
    "zip":"12345"
  },
  "isCartoon":true,
  "IQ":213.5,
  "phones":[
    {
      "type":"lab",
      "number":"212 555-1234"
    },
    {
      "type":"mobile",
      "number":"868 555-1234"
    }
  ],
  "assistant":"Cad Lackey",
  "spouse":null,
  "favorite numbers":[
    2,
    13,
    23,
    66
  ]
}

Notes

* Constants to use for the json_object_to_json_string_ext() function are found in the json-c library documentation.