You may unmarshal into a value of type interface{}
if you don't know anything about the JSON. The encoding/json
package will choose map[string]interface{}
for JSON objects, and []interface{}
for JSON arrays.
You may use a type assertion to check if the result is a map, and you may remove the "foo"
key from it, then marshal it again:
For example:
inputs := []string{`{ "foo": [1,2,3], "bar":"baz"}`,
`{ "foo": "test", "bar123":"baz"}`,
`{ "foo": {"bar":"bar2"}, "bar123":"baz"}`,
`{ "bar123":"baz"}`,
`{ "foo": {"bar":"bar2"}}`,
}
for _, input := range inputs {
var i interface{}
if err := json.Unmarshal([]byte(input), &i); err != nil {
panic(err)
}
if m, ok := i.(map[string]interface{}); ok {
delete(m, "foo") // No problem if "foo" isn't in the map
}
output, err := json.Marshal(i)
if err != nil {
panic(err)
}
fmt.Println(string(output))
}
Output (try it on the Go Playground):
{"bar":"baz"}
{"bar123":"baz"}
{"bar123":"baz"}
{"bar123":"baz"}
{}
If you're sure the input is a JSON object, you can unmarshal directly into a map of type map[string]interface{}
(or even better: into map[string]json.RawMessage
), and so the code will be simpler:
var m map[string]json.RawMessage
if err := json.Unmarshal([]byte(input), &m); err != nil {
panic(err)
}
delete(m, "foo")
Try this one on the Go Playground.
Another optimization is to check if the key "foo"
is actually in the map, and only delete it and marshal the modified map if it is so. Else the input will be the output (no change is required):
var m map[string]json.RawMessage
if err := json.Unmarshal([]byte(input), &m); err != nil {
panic(err)
}
output := input
if _, exists := m["foo"]; exists {
delete(m, "foo")
outputData, err := json.Marshal(m)
if err != nil {
panic(err)
}
output = string(outputData)
}
fmt.Println(output)
Try this one on the Go Playground.