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?

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)))));
}
  • 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 Jul 2 '14 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 Jul 2 '14 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 Jul 2 '14 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 Jul 2 '14 at 12:46
  • thanks, although this is not quite the solution i was looking for it should be very easy to implement. Sorry for not getting back sooner, some how real life managed to get in the way :( – mic Jul 3 '14 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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