Skip to content

This library is for parsing a struct before returning it in JSON format and scrubbing it dynamically of certain fields

License

Notifications You must be signed in to change notification settings

Fyb3roptik/go-json-scrubber

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logo Banner

JSON Scrubber

This library is for parsing a struct before returning it in JSON format and scrubbing it dynamically of certain fields. It supports unlimited fields and can filter them down.

Installation

go get github.com/Fyb3roptik/go-json-scrubber

Usage

jsonscrubber.AddOnly(*interface{}, ...string)
jsonscrubber.RemoveOnly(*interface{}, ...string)

Example

type User struct {
	FirstName         string      `json:"first_name"`
	LastName          string      `json:"last_name"`
	Address           *Address    `json:"address"`
}

type Address struct {
	Address            string    `json:"address"`
	City               string    `json:"city"`
	State              string    `json:"state"`
	Zip                string    `json:"zip"`
}

address := &Address{City: "New York", State: "NY"}
u := &User{FirstName: "Foo", LastName: "Bar", address}

// Initial model fields go here
user := jsonscrubber.AddOnly(u, "first_name", "address").(map[string]interface{})

// You can also do sub structs fields too, this time specifying fields to remove
user["address"] = jsonscrubber.RemoveOnly(u.Address, "address", "state", "zip")

// Return the JSON
b, err := json.MarshalIndent(user)
if err != nil {
		panic(err.Error())
	}
fmt.Print(string(b))

// Return JSON in Revel Framework
return c.RenderJSON(user)

Output

{
	"address": {
		"city": "New York"
	},
	"first_name": "Foo"
}

About

This library is for parsing a struct before returning it in JSON format and scrubbing it dynamically of certain fields

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages