Q:

Unmarshal json optional element Golang

The data I got are xlsx rows in this format: row 1 value 1 is the first cell (A1), row 1 value 2 is the second cell (B1) etc. Any cell can (but doesn't need to) contain styles e.g. Border, Fill etc. .Styles will be in curly brackets like in this data: [[["row 1 value 1",{"Border":null,"Fill":{"arrFill":{"BgColor":"red"}},"Font":null,"ApplyBorder":null}],"row 1 value 2","row 1 value 3"],["row 2 value 1","row 2 value 2","row 2 value 3"]]
How to unmarshal this data to get also the not neccessary style map?
Playground

A:

You may need to use the reflect package to check data types in the resulting array/map. It looks tedious, but seems that is the only way out. I have pasted below some quick and dirt, but runnable code to illustrate the same:
package main

import (
    "encoding/json"
    "fmt"
    "reflect"
)

func parsePrintArray(y interface{}) {
    var dummy []interface{}
    var dummy2 map[string]interface{}
    if reflect.TypeOf(y) == reflect.TypeOf("") {
        fmt.Printf("%s ", y)
        return
    }
    if reflect.TypeOf(y) == reflect.TypeOf(dummy2) {
        x := y.(map[string]interface{})
        for ks, vi := range x {
            fmt.Printf("\ndumping for key %s ", ks)
            if vi != nil {
                parsePrintArray(vi)
            }
        }
        return
    }
    for k, yl := range y.([]interface{}) {
        if reflect.TypeOf(yl) == reflect.TypeOf(dummy) {
            fmt.Println("")
            parsePrintArray(yl.([]interface{}))
        } else if reflect.TypeOf(yl) == reflect.TypeOf("") {
            fmt.Printf("%v: %s ", k, yl.(string))
        } else if reflect.TypeOf(yl) == reflect.TypeOf(dummy2) {
            x := yl.(map[string]interface{})
            for ks, vi := range x {
                fmt.Printf("\ndumping for key %s ", ks)
                if vi != nil {
                    parsePrintArray(vi)
                }
            }
        }
    }
}

func main() {

    x := "[[[\"row 1 value1\",{\"Border\":null,\"Fill\":{\"arrFill\":{\"BgColor\":\"red\"}},\"Font\":null,\"ApplyBorder\":null}],\"row 1 value2\",\"row 1 value 3\"],[\"row 2 value 1\",\"row 2 value 2\",\"row 2 value 3\"]]"

    var y []interface{}
    err := json.Unmarshal([]byte(x), &y)
    if err != nil {
        panic(err)
    }
    parsePrintArray(y)
    fmt.Println("")
}

