jsoner
is a Go package that provides utilities for marshaling Go structs into JSON with advanced filtering capabilities. It allows you to exclude specific fields or nested paths from the JSON output.
To install the package, run:
go get github.com/go-universal/jsoner
Import the package in your Go code:
import "github.com/go-universal/jsoner"
Converts the input value (v
) into JSON format, excluding specified fields or paths.
func Marshal(v any, exclude ...string) ([]byte, error)
type Book struct {
Title string
ISBN string `json:"isbn"`
}
type Author struct {
Name string `json:"name"`
Family string `json:"family"`
Books []Book `json:"author_books"`
}
author := Author{
Name: "John",
Family: "Doe",
Books: []Book{
{Title: "Book 1", ISBN: "12345"},
{Title: "Book 2", ISBN: "67890"},
},
}
jsonData, err := jsoner.Marshal(author, "family", "author_books.isbn")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
// Output: {"name":"John","author_books":[{"Title":"Book 1"},{"Title":"Book 2"}]}
Converts the input value (v
) into indented JSON format, excluding specified fields or paths.
func MarshalIndent(v any, indent string, exclude ...string) ([]byte, error)
type Book struct {
Title string
ISBN string `json:"isbn"`
}
type Author struct {
Name string `json:"name"`
Family string `json:"family"`
Books []Book `json:"author_books"`
}
author := Author{
Name: "John",
Family: "Doe",
Books: []Book{
{Title: "Book 1", ISBN: "12345"},
{Title: "Book 2", ISBN: "67890"},
},
}
jsonData, err := jsoner.MarshalIndent(author, " ", "family", "author_books.isbn")
if err != nil {
panic(err)
}
fmt.Println(string(jsonData))
// Output:
// {
// "name": "John",
// "author_books": [
// {
// "Title": "Book 1"
// },
// {
// "Title": "Book 2"
// }
// ]
// }
This project is licensed under the ISC License. See the LICENSE file for details.