https://github.com/jc21/json-strip-comments
A command to strip c style comments from a given file
https://github.com/jc21/json-strip-comments
golang json jsonc
Last synced: about 2 months ago
JSON representation
A command to strip c style comments from a given file
- Host: GitHub
- URL: https://github.com/jc21/json-strip-comments
- Owner: jc21
- Created: 2022-04-26T04:07:11.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-01-09T05:17:55.000Z (over 3 years ago)
- Last Synced: 2025-03-20T09:50:52.459Z (over 1 year ago)
- Topics: golang, json, jsonc
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# JSON Strip Comments
This is a very simple command that will remove comments from JSON files.
_Strictly speaking this could be used on any text file not just JSON, however
the project's goal and logic is centered around JSON files._
```bash
Removes c style comments from a json file
Usage: json-strip-comments [--output OUTPUT] [--empty] [FILENAME]
Positional arguments:
FILENAME
Options:
--output OUTPUT, -o OUTPUT
Output to given filename instead of stdout
--empty, -e Remove empty lines after comment removals
--help, -h display this help and exit
```
## Caveats
Due to the simplicity of this project, only comments with whitespace preceeding them
will be removed. This prevents removing false positive comments from the inside of a
json string for example.
```jsonc
// this comment will be removed
{
// and this one too
"id": 1,
/* and so will this */
"name": "Bobby Tables",
/*
Multi line comments are removed as well \o/
*/
"email": "user@example.com", // however, this comment will NOT be removed
"enabled": true /* and neither would this one */
}
```
## Usage Examples
#### Piping stdin
```bash
cat somefile.jsonc | json-strip-comments
```
#### Removing empty lines
```bash
cat somefile.jsonc | json-strip-comments -e
```
#### Specify an input file
```bash
json-strip-comments -e something.jsonc
```
#### Specify an output file
```bash
cat input.jsonc | json-strip-comments -e -o "output.json"
# or
json-strip-comments -e -o "output.json" "input.jsonc"
```
#### Using wildcards in output filename
This only workls when using the incoming filename
```bash
json-strip-comments -e -o "[folder][file].json" "/path/to/input.jsonc"
# writes to: /path/to/input.json
```
#### Perform on all files in current folder
This involves a bit of bash magic, it looks for all `.jsonc` files
and writes `.json` files with the same name in the same place.
```bash
find ./ -type f -name "*.jsonc" -exec json-strip-comments -e -o "[folder][file].json" "{}" \;
```
## Install
#### Centos
RPM hosted on [yum.jc21.com](https://yum.jc21.com)
#### Go Install
```bash
go install github.com/jc21/json-strip-comments@latest
```
#### Building
```bash
git clone https://github.com/jc21/json-strip-comments && cd json-strip-comments
go build -o bin/json-strip-comments cmd/json-strip-comments/main.go
./bin/json-strip-comments -h
```