1

I have a JSON file in which the value of each key also contains JSON data. So I have tried to work with the toplevel keys in the main file and tried work and parse the nested data structure to some other go files.

main.go

//Unmarshal the main json
var data map[string][]interface{}
json.Unmarshal(someByte, data)

//Sending the value to be parsed in a different file if necessary
var key1 = Mdata["key1"]
NewKey1StructType().GetMethod(&key1)

key1.go

// type and constructor
type Key1Struct struct {
}
func NewKey1StructType() *Key1Struct {
    return &Key1Struct{}
}
//GetMethod
//type that has the structure of the value of key1
type ValueType stuct {
 val1 string `json: ......`
......
}
func (s Key1Struct) GetMethod(i *[]interface{}) {
    var m ValueType
    json.Unmarshal(i, &m)
// ................| <- It expects []byte instead of *[]interface{}

data.json

{
   "key1" : [ {"key1a":"value1a"},{"key1b":"value1b"} ],
   "key2" : {"key2a":"value2a"} ,
   ........
   "keyN" : [{"keyNa":"valueNa"},{"keyNb":"valueNb"}]
}

Where,

  • string = key1
  • []interface{} = [{"keyNa":"valueNa"},{"keyNb":"valueNb"}]

How can I unmarshal *[]interface{} in this kind of situation?

arif
  • 579
  • 1
  • 7
  • 21
  • 2
    I don't quite understand why you would pass `*[]interface{}` to `GetMethod`. Also please add a sample JSON input to your question – blackgreen Apr 13 '22 at 20:36
  • @blackgreen may be due to my lack of understanding. There are some text (!JSON) contents in the values so I can just Sprint them from the `[]interface{}`. How would you do it? I mean how would you pass the `key1`s value to the method? BTW the values themselves are very large (200MB-2GB). – arif Apr 13 '22 at 20:42
  • 1
    the value associated to `key2` in your sample is an object, so `map[string][]interface{}` seems incorrect. You meant it as an array maybe? You might want to look into [`json.RawMessage`](https://pkg.go.dev/encoding/json#RawMessage). – blackgreen Apr 13 '22 at 20:48
  • 1
    then, if the input is really that big, you might want to look into streaming it. [1](https://stackoverflow.com/questions/31794355/decode-large-stream-json/42612995#42612995), [2](https://stackoverflow.com/questions/44307219/go-decode-json-as-it-is-still-streaming-in-via-net-http) – blackgreen Apr 13 '22 at 20:51
  • 1
    @blackgreen `json.RawMessage` looks like another approach but I have to rewrite the whole thing. But it's a considerable approach. Thank you. – arif Apr 13 '22 at 21:03
  • Yes, `json.RawMessage` is the option provided by `encoding/json` to support partially unmarshalling and deferring JSON processing. You could also provide custom types that support appropriate decoding for their JSON (either struct tags, or an `UnmarshalJSON` method. I would avoid decoding into `interface{}` if you can -- it just creates more work and more chance of bugs. – mpx Apr 15 '22 at 07:18

0 Answers0