简体   繁体   中英

Remove key from JSON dynamically in golang

I have a varying JSON schema ( json.RawMessage ) that can have an arbitrary format. I do not know the format at compile time.

In golang, I would like to check if in the root-JSON object a key exists and if so remove that key completely and deserialize.

So for example, say I need to remove "foo" if it exists

{ "foo": [1,2,3], "bar123":"baz"} -> {"bar123":"baz" }
{ "foo": "test", "bar123":"baz"} -> { "bar123":"baz" } 
{ "foo": {"bar":"bar2"}, "bar123":"baz"} -> { "bar123":"baz" }
{ "bar123":"baz"} -> { "bar123":"baz" }
{ "foo": {"bar":"bar2"}} -> {}

Given that I need to know the structure of the JSON in advance for serializing and deserializing, how can I do this with go?

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 .

Personally i love to use gabs module which allows to handle those kind of situation in a more human friendly way.


For installing the module use:

go get github.com/Jeffail/gabs/v2

So, for remove keys dinamicaly if exists

// jsonParsed var contains a set of functions to play arround
jsonParsed, _ := gabs.ParseJSON([]byte(`{
    "outter":{
        "inner":{
            "value1":10,
            "value2":22
        },
        "alsoInner":{
            "value1":20
        }
    }
}`))

// for this case, it's useful Exists or ExistsP functions
exists := jsonParsed.Exists("outter", "inner", "value1")
// exists == true

exists = jsonParsed.ExistsP("outter.inner.value3")
// exists == false

// for delete a key ... you can use Delete or DeleteP functions.
// this validate for you whether or not the key exists
jsonParsed.DeleteP("outter", "inner")

// and check it with a nice pretty-print
fmt.Println(jsonParsed.StringIndent("", "  "))
// "outter: {
//     "alsoInner: {
//        "value: 20
//     }
// }

Take a look at the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM