Skip to content

Instantly share code, notes, and snippets.

@current1990
Created April 11, 2020 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save current1990/73e8a121ef5d85266db23daf12638074 to your computer and use it in GitHub Desktop.
Save current1990/73e8a121ef5d85266db23daf12638074 to your computer and use it in GitHub Desktop.
remove json fields
package main
import (
"os"
"encoding/json"
"io/ioutil"
"fmt"
"strings"
)
var (
filename string
rm_fields []string
)
func parseArg() {
if len(os.Args) != 3 {
fmt.Println("Usage ./a.out $filename $rmFields")
panic(0)
}
filename = os.Args[1]
rm_fields = strings.Split(os.Args[2], ",")
}
func main() {
parseArg()
data, e := ioutil.ReadFile(filename)
if e != nil {
panic(e)
}
var tmp map[string]interface{}
e = json.Unmarshal(data, &tmp)
if e != nil {
panic(e)
}
for _, field := range rm_fields {
delete(tmp, field)
}
out, e := json.Marshal(tmp)
if e != nil {
panic(e)
}
fmt.Println(string(out))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment