JSON proprocessor with comments

98 views
Skip to first unread message

Alexander Lehmann

unread,
Jul 15, 2017, 8:21:25 AM7/15/17
to Packer
As Packer is using json for the config, I would like to run a preprocessor before running packer to strip Javascript comments from the json file.

I have found a nodejs tool for that called strip-json-comments-cli which works ok, but since that requires npm install, it seems like kind of complicated to provision e.g. in a CI environment just for the single script.

On the other hand I could add the stripped file in the git repo so that as long as there are no changes in the source file, the tool is not needed (which would solve the CI question).

Can anyone suggest other tools (e.g. in go) or are you using another procedure to get comment support for the config file?


Thanks, Alexander


Rickard von Essen

unread,
Jul 15, 2017, 8:50:58 AM7/15/17
to packe...@googlegroups.com
I guess jq should work (depending on what you mean by comments). Check delpath(). jq is available in most distributions/OS'es package management system. 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/112680f0-eeba-4c93-886f-0e578058525c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexander Lehmann

unread,
Jul 15, 2017, 10:40:11 AM7/15/17
to Packer
I would prefer Javascript/Java-live comments with // or /**/, which jq doesn't allow i think



On Saturday, July 15, 2017 at 2:50:58 PM UTC+2, Rickard von Essen wrote:
I guess jq should work (depending on what you mean by comments). Check delpath(). jq is available in most distributions/OS'es package management system. 
On Jul 15, 2017 14:21, "Alexander Lehmann" <alex...@gmail.com> wrote:
As Packer is using json for the config, I would like to run a preprocessor before running packer to strip Javascript comments from the json file.

I have found a nodejs tool for that called strip-json-comments-cli which works ok, but since that requires npm install, it seems like kind of complicated to provision e.g. in a CI environment just for the single script.

On the other hand I could add the stripped file in the git repo so that as long as there are no changes in the source file, the tool is not needed (which would solve the CI question).

Can anyone suggest other tools (e.g. in go) or are you using another procedure to get comment support for the config file?


Thanks, Alexander


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Gabo Kete

unread,
Jul 15, 2017, 4:43:09 PM7/15/17
to Packer
Hi Alexander

I hope I have understood correctly your question... I do have in my JSON files one line of comments right before I start the definition of my variables.. this is the way I have added them

{
  "_comment": "This si a centos7-x64 Machine for VmWare ESXi 6.0", <=== My comments
  "variables": {
    "nameVm": "centos-7.1-vmware_DevOPS",
    "ks_file": "simple-ks-integrado-CIS-BitbyBit.ks",
    "ks_server": "192.168.1.150",
    "ks_path": "centos7.1511_x64/ks/",
    "vmware_host": "192.168.1.250",
    "http_dir": "/packer"
}

Basically is to add the "_comment" style element in the JSON file at the very beginning. I do use this to describe what the is the purpose of the whole JSON file

The other way I have found myself using comments and not affect the JSON file, is using JSMIN 

Hope this helps

Alexander Lehmann

unread,
Jul 17, 2017, 6:48:33 AM7/17/17
to Packer
Thanks for the suggestions, I am now using a shell script to preprocess the file like this:

jsmin <$1 | jq '{_generated:"this file was generated, please edit the source file \"'$1'\" instead"}+.' >`basename $1 .json`_nocomments.json

that works quite well.

Gabo Kete

unread,
Jul 17, 2017, 5:52:15 PM7/17/17
to Packer
Glad that helped... the only downside of jsmin is that remove the nice formatted file for one that is on liner file... but I guess you have managed well by adding the commento to update/edit the source file , which I reckon you may have it in a Subversion Control Server ;)  
Reply all
Reply to author
Forward
0 new messages