duai3681 2019-08-01 11:25
浏览 112
已采纳

在golang中从JSON动态删除密钥

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?

  • 写回答

1条回答 默认 最新

  • douzhi6160 2019-08-01 11:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?