Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I beautify JSON programmatically? [closed]

Tags:

json

php

You might say this is duplicate of this question, but original question WASN'T answered there. Important part of question is: programmatically?

Is there any php function? Native or homemade?

like image 394
genesis Avatar asked Jul 12 '11 23:07

genesis


People also ask

How do I beautify a JSON file?

To beautify your JSON file, you can go to https://jsoneditoronline.org, and paste your JSON there, or load a JSON file from disk or from an url.

How do I make my JSON response pretty?

Use JSON. stringify(obj) method to convert JavaScript objects into strings and display it. Use JSON. stringify(obj, replacer, space) method to convert JavaScript objects into strings in pretty format.

How do I beautify JSON in Notepad ++?

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.


2 Answers

json_encode() has a flag JSON_PRETTY_PRINT

echo json_encode($data, JSON_PRETTY_PRINT); 
like image 174
Mārtiņš Briedis Avatar answered Oct 02 '22 21:10

Mārtiņš Briedis


had the same question right now. But as you also i'm having php < 5.4. Zend Framework has Zend_Json::prettyPrint(). Works very well.

like image 30
German Avatar answered Oct 02 '22 20:10

German