简体   繁体   中英

Type switch to []map[string]interface{} on the value of map[string]interface{}

Problem

I'm facing the issue to remove not required arrays from an json object eg. arrays with only one element which is not an object or array. (No arrays as root of the input)

Example

In:

{"name": [{ "inner": ["test"] }]}

Wanted Out:

{"name": [{ "inner": "test" }]}

Approach

I started with a simple type switch on the values of a parsed map[string]interface{} and recognized that it wont switch to the case []map[string]interface{} . (Given example)

So here is the implementation I came up with. It works for most of the scenarios but not for inner Objects within an array yet.

type jsonMap map[string]interface{}
type jsonMapList []map[string]interface{}

m := jsonMap{}

err := json.Unmarshal(s, &m)
if err != nil {
    panic(err)
}

res := removeFromObject(m)

bytes, err := json.Marshal(res)
if err != nil {
    panic(err)
}
result := string(bytes)
log.Infof("Parse Result: %s", result)

func removeFromObject(in jsonMap) jsonMap {
    res := jsonMap{}

    for k, v := range in {
        switch value := v.(type) {
        case jsonMap:
            res[k] = removeFromObject(value)
        case jsonMapList:
            list := []jsonMap{}
            for _, entry := range value {
                list = append(list, removeFromObject(entry))
            }
            res[k] = list
        case []interface{}:
            if len(value) == 1 {
                res[k] = value[0]
            } else {
                res[k] = value
            }
        default:
            res[k] = value
        }
    }

    return res
}

Question

How do I switch case to an object array, so that I can recursively resolve the objects within that array too?

You can use this function to remove the undesired arrays.

func removearr(x interface{}) interface{} {
    switch val := x.(type) {
    case map[string]interface{}:
        for k, v := range val {
            val[k] = removearr(v)
        }
        return val
    case []interface{}:
        if len(val) == 1 {
            // remove array only if the value is not an object
            if _, ok := val[0].(map[string]interface{}); !ok {
                return removearr(val[0])
            }
        }

        for i, v := range val {
            val[i] = removearr(v)
        }
        return val
    default:
        return x
    }
}

https://play.golang.com/p/mwo7Y2rJ_lc

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