4

I'm using PhpStorm and I have several JSON files that are fully formatted with indents and new lines.

Is there a plugin I can get for PhpStorm that will allow me to collapse/minify all the JSON in these files?

1 Answer 1

4

First thought: Cannot PHP do that itself? Just citing from my memory:

$json_file = '/path/to/file.json';  /* dynamically set the name of your JSON file here */
file_put_contents($json_file,       /* Write back to disk */
  json_encode(                      /* Re-Encode to JSON */
    json_decode(                    /* Decode original JSON object */
      file_get_contents($json_file) /* Read the original JSON file from disk */
    )
  )
);

No safeguard in yet, but of course you can split that up to only write the file if no errors have occured. Reading above code bottom-up, you

  1. read the original file (including indents etc) from disk
  2. decode it to make it a PHP object (indents etc. lost here)
  3. encode it back to JSON (no intents, as they were lost in previous step – this will result in a one-line string)
  4. write the newly created (one-liner) JSON back to disk.

Put the entire thing in a loop to process all your JSON files:

foreach ( glob('/path/to/dir/*.json') as $json_file ) {
  file_put_contents($json_file,json_encode(json_decode(file_get_contents($json_file))));
}

Do we really need a plugin for what can be done by a one-liner? ;)

EDIT: As ComFreek pointed out in his comment, json_decode() only deals with UTF-8. So if your files use a different charset, you might want to add a call to utf8_encode() to wrap around file_get_contents(), so the "innermost command" of above loop reads utf8_encode(file_get_contents($json_file)). for completeness:

foreach ( glob('/path/to/dir/*.json') as $json_file ) {
  file_put_contents($json_file,json_encode(json_decode(utf8_encode(file_get_contents($json_file)))));
}
6
  • I would be careful in regards to charsets and encodings. json_decode() only deals with UTF-8 (source). If the files are already encoded in ASCII or UTF-8, the code will perfectly work.
    – ComFreek
    Commented Jul 2, 2014 at 9:28
  • @ComFreek thanks for pointing this out! I wasn't aware of this, as I usually deal with UTF8 only when it comes to JSON. Updated my answer accordingly, giving you credit of course :)
    – Izzy
    Commented Jul 2, 2014 at 12:23
  • I'm sorry to bother you again, but utf8_encode() doesn't do what you probably think it does (blame PHP). It requires the input string to be in ISO-8859-1. One must use iconv() to convert between character encodings.
    – ComFreek
    Commented Jul 2, 2014 at 12:43
  • 1
    Yeah, all those nasty details :) Well, we're on SoftwareRecs here. I didn't even recommend software, which was quite a little borderline. But we shouldn't dig to deep into programming here. Thanks again for pointing out a culprit, @ComFreek – but I guess if the OP wants to go this path and needs more details, that's rather a case for Stack Overflow then :)
    – Izzy
    Commented Jul 2, 2014 at 12:46
  • 1
    Glad I was able to help! In case you really need to deal with other charsets, please take a look at iconv (as suggested by ComFreek) and recode (always good to have an alternative available ;) And someone should debug that realLife() procedure to figure out why it always comes in between #D
    – Izzy
    Commented Jul 3, 2014 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.