Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I beautify JSON programmatically? [duplicate]

Do you know of any "JSON Beautifier" for JavaScript?

From

{"name":"Steve","surname":"Jobs","company":"Apple"} 

To

{   "name" : "Steve",   "surname" : "Jobs",   "company" : "Apple" } 

Example

some_magic(jsonObj); // return beautified JSON 
like image 265
Randy Mayer Avatar asked Apr 10 '10 20:04

Randy Mayer


People also ask

How do I beautify JSON in Notepad ++?

To install the plugin do the following steps: Open notepad++ -> ALT+P -> Plugin Manager -> Selcet JSON Viewer -> Click Install. Restart notepad++ Now you can use shortcut to format json as CTRL + ALT +SHIFT + M or ALT+P -> Plugin Manager -> JSON Viewer -> Format JSON.

How do I beautify JSON data in Python?

To write a Python object as JSON formatted data into a file, json. dump() method is used. Like json. dumps() method, it has the indents and separator parameters to write beautified JSON.

How do I beautify JSON in Vscode?

Install via package manager. Hit F1 or "cmd+shift+p" and type install and then type Prettify JSON and hit enter.


1 Answers

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 formatting solution

If you don't need to do it programmatically, Try JSON Lint. Not only will it prettify your JSON, it will validate it at the same time.

like image 104
Andy E Avatar answered Sep 29 '22 18:09

Andy E