Created
April 11, 2020 15:02
-
-
Save current1990/73e8a121ef5d85266db23daf12638074 to your computer and use it in GitHub Desktop.
remove json fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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