How can I beautify JSON programmatically? [duplicate]

Programmatic formatting solution: The JSON.stringify method supported by many modern browsers (including IE8) can output a beautified JSON string: JSON.stringify(jsObj, null, “\t”); // stringify with tabs inserted at each level JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level Demo: http://jsfiddle.net/AndyE/HZPVL/ This method is also included with json2.js, for supporting older browsers. Manual … Read more

How to turn off the Eclipse code formatter for certain sections of Java code?

Eclipse 3.6 allows you to turn off formatting by placing a special comment, like // @formatter:off … // @formatter:on The on/off features have to be turned “on” in Eclipse preferences: Java > Code Style > Formatter. Click on Edit, Off/On Tags, enable Enable Off/On tags. It’s also possible to change the magic strings in the … Read more

How to prettyprint a JSON file?

The json module already implements some basic pretty printing in the dump and dumps functions, with the indent parameter that specifies how many spaces to indent by: >>> import json >>> >>> your_json = ‘[“foo”, {“bar”:[“baz”, null, 1.0, 2]}]’ >>> parsed = json.loads(your_json) >>> print(json.dumps(parsed, indent=4, sort_keys=True)) [ “foo”, { “bar”: [ “baz”, null, 1.0, … Read more

pretty-print JSON using JavaScript

Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use: var str = JSON.stringify(obj, null, 2); // spacing level = 2 If you need syntax highlighting, you might use some regex magic like so: function syntaxHighlight(json) { if (typeof json != ‘string’) { json = JSON.stringify(json, undefined, … Read more