format json

Ketan Jetty
enthusiasm for technology

format json

In most of the cases, you will receive a JSON string in a flat format from the server. A continous flat JSON string is hard to read and debug. The following code is developed in JavaScript to format a JSON string. This same code can be also used to create an indent JSON function in coldfusion using CFSCRIPT.

If you have an input JSON string like this:

JSON code
{"user":{"id":{"type":"integer","default":"","key":"primary","required":true,"unique":false,"mapping":"id","value":""},"created_date":{"type":"timestamp","default":"current_timestamp","required":true,"unique":false,"mapping":"created_date","value":""},"updated_date":{"type":"timestamp","default":"current_timestamp","required":true,"unique":false,"mapping":"updated_date","value":""},"is_active":{"type":"bit","default":"undefined","required":true,"unique":false,"mapping":"is_active","value":""},"username":{"type":"varchar","maxlength":255,"default":"","required":true,"unique":false,"mapping":"username","value":""}}}

The indent JSON function will convert it like this:

Formatted JSON code
{
    "user":{
        "id":{
            "type":"integer",
            "default":"",
            "key":"primary",
            "required":true,
            "unique":false,
            "mapping":"id",
            "value":""
        },
        "created_date":{
            "type":"timestamp",
            "default":"current_timestamp",
            "required":true,
            "unique":false,
            "mapping":"created_date",
            "value":""
        },
        "updated_date":{
            "type":"timestamp",
            "default":"current_timestamp",
            "required":true,
            "unique":false,
            "mapping":"updated_date",
            "value":""
        },
        "is_active":{
            "type":"bit",
            "default":"undefined",
            "required":true,
            "unique":false,
            "mapping":"is_active",
            "value":""
        },
        "username":{
            "type":"varchar",
            "maxlength":255,
            "default":"",
            "required":true,
            "unique":false,
            "mapping":"username",
            "value":""
        }
    }
}

formatJson()
// formatJson() :: formats and indents JSON string
function formatJson(val) {
	var retval = '';
	var str = val;
    var pos = 0;
    var strLen = str.length;
	var indentStr = '    ';
    var newLine = '<br />';
	var char = '';
	
	for (var i=0; i<strLen; i++) {
		char = str.substring(i,i+1);
		
		if (char == '}' || char == ']') {
			retval = retval + newLine;
			pos = pos - 1;
			
			for (var j=0; j<pos; j++) {
				retval = retval + indentStr;
			}
		}
		
		retval = retval + char;	
		
		if (char == '{' || char == '[' || char == ',') {
			retval = retval + newLine;
			
			if (char == '{' || char == '[') {
				pos = pos + 1;
			}
			
			for (var k=0; k<pos; k++) {
				retval = retval + indentStr;
			}
		}
	}
	
	return retval;
}

coldfusion


CF Quick Reference


Ginger CMS
the future of cms, a simple, easy and intutive content management system ... more


CFTurbine
cf prototyping engine, generates boilerplate code and ... more


Jrun monitor
monitor and timely auto-restart to avoid Jrun hang ... more


Inheritance Config.
uses OOPs inheritance to create configuration file ... more


Real Estate App.
complete real estate application using data from MLS ... more


Search Engine Lite
create your own search engine for your web site ... more