Jump to content

[SOLVED] Beautify JSON


scottybwoy

Recommended Posts

Hi REGEX Lords,

 

I'd like to beautify my JSON files so it's a bit easier to read. json_encode and fwrite, just sticks it all on on line, so I'd like to add in some /n and /t to break it up a bit.

 

So I thought once I've got my JSON encoded object I could use some preg_replace magic to do some stuff like changing

{ => {/n/t
} => }/n
: => " : " // But without the quotes, just wanted to space it out a bit
, => ,/n // and would it be possible to have the cursor level to the line above it?

Thanks for your help, this may be a good one to put up on some Regex libraries too, what do ya think ;)

Link to comment
https://forums.phpfreaks.com/topic/85510-solved-beautify-json/
Share on other sites

From the sample data you provided that consisted of about 1 character each, nothing was beside them. Simply using str_replace() should do the trick. :)

 

Oh maybe there are situations where str_replace() would replace unwanted things, well provide a better example then. Because not everyone is so familiar with JSON to know what you mean. Duh?

Link to comment
https://forums.phpfreaks.com/topic/85510-solved-beautify-json/#findComment-436820
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.