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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.