// This is really not meant as a json-creator, it's more an experiment in carried state function JSONify(stream){ if(stream.hasOwnProperty('isStream')){ var i, j, indents = '', endIndents = '', objectToString = Object.prototype.toString, arrayString = '[object Array]', isArray = objectToString.call(stream.data) === arrayString , isObject = typeof(stream.data) === 'object', propIsObject, propIsArray, propValue; stream.output += (isArray ? "[ \n" : isObject ? "{ \n" : ''); for(i = 0; i < stream.indent; i++){ indents += " "; } for(i = 0; i < stream.indent - 1; i++){ endIndents += " "; } for(i in stream.data){ if(stream.data.hasOwnProperty(i)){ propValue = stream.data[i]; propIsObject = typeof(propValue) === 'object'; propIsArray = objectToString.call(propValue) === arrayString stream.output += indents + (stream.parentIsArray ? '' : '"' + i + '": '); if(propIsObject){ stream.output += arguments.callee({ isStream: true, parentIsArray: propIsArray, data: propValue, indent: stream.indent + 1, output: '' }).output + ",\n"; } else { stream.output += '<b>' + (isNaN(propValue) ? '"' : '') + propValue + (isNaN(propValue) ? '"' : '') + '</b>,' + "\n"; } } } stream.output = stream.output.substring(0, stream.output.length-2) + "\n" + endIndents + (isArray ? "]" : isObject ? "}" : ''); } else { return arguments.callee( { isStream: true, data: stream, indent: 1, output: '' } ); } return stream; } document.body.innerHTML += '<pre>' + JSONify({ name: 'drew' ,age: 24 ,obj: { prop1: 'another' ,prop2: 'try another' } ,arr: [ 'a', 'b', 'c' ] ,arr2: [3,4,5,6] }).output + '</pre>';