jsonc is an extension of the JSON data format that allows single and multi line comments.
To install jsonc, use go get:
go get github.com/fpatron/jsonc- Comment Support: Easily parse JSON files with single-line and multi-line comments.
- Simple API: Uses a familiar API similar to the standard
encoding/jsonpackage.
Here's a basic example of how to use jsonc:
package main
import (
"fmt"
"log"
"os"
"github.com/fpatron/jsonc"
)
func main() {
data, err := os.ReadFile("example.json")
if err != nil {
log.Fatalf("Failed to read example.json: %v", err)
}
var example ExampleData
err = jsonc.Unmarshal(data, &example)
if err != nil {
log.Fatalf("Failed to parse example.json: %v", err)
}
fmt.Printf("Parsed data: %+v\n", example)
}for a complete example checkout this example code
A separate jsonc/v2 module is available for projects that want to use Go's experimental encoding/json/v2 backend. It exposes the same behaviour with the richer v2 Unmarshal(data, v, ...json.Options) signature.
Note:
encoding/json/v2requires Go 1.26 and theGOEXPERIMENT=jsonv2build flag.
See the v2 README for installation instructions, a full API comparison, and usage examples.