fkei / JSON.minify

Simple minifier for JSON to remove comments and whitespace (node.js and browser)

Home Page:http://blog.getify.com/2010/06/json-comments/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A command-line utility to remove quickly comments from a json would be of great use

jmfayard opened this issue · comments

Hello fkei,

I need to be able to minify a given json file from the command-line
(I'm on macosx, but with homebrew, node, python, ... everything installed) so that I can do

$ jsonminify manifest-with-comments.json --output manifest.json

actually, that would be even best if the command-line utility would be clever enough to just Do What I Want. "Oh, the filename follows this -with-comments.json conention". So the following command would be a shortcut as the one before

$ jsonminify manifest-with-comments.json

manifest-with-comments.json being here a file in my little free software project https://github.com/internaciulo/search-tab-in-chrome-s-omnibox

I opened an issue there getify#13

@getify replied: No such command-line tool exists in this project. I would suggest asking the maintainer of that npm package if they'll make a command-line tool. It's pretty easy to do, but it just hasn't been done here. I would accept a pull request from someone if they implemented a good one.

Hence this new issue here.

Arigato!

commented

Do you want to run from the command line JSON.minify?

Well, I have already made a thing called mjson https://github.com/fkei/node-mjson

Please try it here.

Wow great !

Well excepted a few details
(but as always, details are what matters most)

$ cat ./test/comment.json | mjson.js

People with unix background will warn you that this is a Useless Use of Cat : http://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat)

No problem, this does the same job
$ mjson.js < ./test/comment.json

But of course we can do better

$ mjson.js ./test/comment.json

I didn't try if it's possible, but mjson.js --help seems to imply that no.

I hope this is not a restriciton of node.js, that would be weird :(

But wait, standard ouput is fine for using with demo, but it is not what you use once you understand it. More like

$ mjson.js manifest-with-comments.json > manifest.json

or better

$ mjson.js manifest-with-comments.json -o manifest.json

But then there is the suggestion I made, inspired by Ruby on Rails's Convention over configuration principle
http://en.wikipedia.org/wiki/Convention_over_configuration

$ mjson.js manifest-with-comments.json # does the same thing as before since the pattern is recognized

This would be great because:

  • All projects will use the same convention
  • People that will enter the project will be less confused on why there are two json files
  • With this you can do something like
    $ find . -name "*-with-comments.json" | xargs mjson.js
  • Then you can let git triggers it everytime you commit

Then this has to be documented of course, so a documentatinon pull request has to be opened on
getify#13

I am sure this would be really useful, I hope this is not too much to ask :-)

@internaciulo

Since mjson is a separate project by @fkei I think the request to change the behavior of that utility belongs there in that repo rather than in this thread for JSON.minify. Again, if someone decides at some point to put in a Pull-Request to my main repo that adds in a CLI utility, I'll be happy to consider it. But until then, it's not really a JSON.minify issue.

Also, note: it's typical that when you make such requests (to change someone's existing code), you actually put in a Pull-Request with the changes implemented, instead of just expecting the author to change something to your desires. If a utility exists, but it's not exactly what you want, and you can't make the changes to his code, I'd suggest you instead just write a little bash-script wrapper to conform it to your desired usage.

This is very off topic, but since you bring it there:

Also, note: it's typical that when you make such requests (to change someone's existing code), you actually put in a Pull-Request with the changes implemented, instead of just expecting the author to change something to your desires. I

I do know that it's typical, but it's really sad, since bug trackers and pull request are aimed primarily at tracking and correcting, well, bugs

But ideas, design, feedback are as bit important as code and bugs and it's just awkard to discuss them with pull requests and bug trackers

That's why many free software projects that could be great never succeed.

They just don't use the apropriate tools like http://uservoice.com/

I promise that I would never use bug trackers to discuss things that are not bugs if I had a choice.

But that's just not the cas right now.

commented

I'll implement this future when I find time.

Great 👍

commented

Hi

Added a specification that had requested. Please look by using all means.

Document: https://github.com/fkei/node-mjson#command-line-options
Commit : fkei/node-mjson@8bb2acb

$ npm install mjson@0.4.1

Finally, I agree completely with @getify opinion.

Hello fkey,

well first thanks a lot 👍

I have so much projects right now, that I can't do much right now.

But I promise I will look at what you did, see what I can do to improve it or just tell the world if it is already perfect :-D