Input:
"data": {
"filter": [
"["EQ","foo",0],["LIKE","baz","%.2%"],["IN","bar",[1,5,9,11]],["EQ","myBool",true]"
],
"limit": [
"101"
],
"offset": [
"0"
],
"sort": [
"id:ASC"
]
}
Struct:
type MyStruct struct {
Operator string
Field string
Values []interface{} //this could be an array of strings or int64
}
Test Code:
var parsed [][]interface{}
var parsedValues []MyStruct
if err := json.Unmarshal([]byte(filters), &parsed); err != nil {
fmt.Println("FAILED TO UNMARSHALL FILTERS : " + err.Error())
}
for _, y := range parsed {
var myStuff MyStruct
var values []interface{}
for idx2, col := range y {
if idx2 == 0 {
myStuff.Operator = col.(string)
} else if idx2 == 1 {
myStuff.Field = col.(string)
} else {
values = append(values, col)
}
}
myStuff.Values = values
parsedValues = append(parsedValues, criteria)
}
EDITED:
I am hitting an error in un marshaling.
FAILED TO UNMARSHALL FILTERS : invalid character ',' after top-level value
Is it expected that the commas between arrays in this string will mess up the parse? I tried to do a replace to remove commas between each sub array ... but ... now it just yells about the [.
Maybe this is the wrong approach altogether. I thought the answer below was going to work but with my input it didnt. I am not 100% sure where the disconnect is.
Any help with this would be appreciated.