Prettify JSON Array in JavaScript
html, javascript, jquery, json
Solution
You may use JSON.stringify:
JSON.stringify(jsonobj,null,'\t')
See the demo.
UPDATE: If you don't have `jsonobj`,but have json string,then before using `stringify` function,convert json string to json object by this line:
jsonobj = JSON.parse(jsonstring);
Problem
Possible Duplicate: JSON pretty print using JavaScript I'm working on a project that will be used to help analyse and understand JSON arrays by future developers of a platform. I'm referencing Facebook's brilliant Graph Explorer page, seen here, and want to output our array in a prettified, correctly tab indented and line breaker array, just as it does on the explorer. The arrays are outputted to a `textarea`, and because of this I think I'm running into issues with the line breaking and tabbing. I've also tried using the prettify library, but with no luck. Example: ``` {"outcome" : "success", "result" : {"name" : "messaging-sockets", "default-interface" : "external", "include" : [], "socket-binding" : {"messaging" : {"name" : "messaging", "interface" : null, "port" : 5445, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}, "messaging-throughput" : {"name" : "messaging-throughput", "interface" : null, "port" : 5455, "fixed-port" : null, "multicast-address" : null, "multicast-port" : null}}}, "compensating-operation" : null} ``` To: ``` { "outcome":"success", "result":{ "name":"messaging-sockets", "default-interface":"external", "include":[ ], "socket-binding":{ "messaging":{ "name":"messaging", "interface":null, "port":5445, "fixed-port":null, "multicast-address":null, "multicast-port":null }, "messaging-throughput":{ "name":"messaging-throughput", "interface":null, "port":5455, "fixed-port":null, "multicast-address":null, "multicast-port":null } } }, "compensating-operation":null } ```