Unmarshaling json dynamic field

Greetings to all how much JSON wrote into the GO structure did not come across such, you can tell how to write a JSON field in the structure if it changes dynamically, for example,
JSON contains an array of objects, but the name of the object can be any, so I can not specify it as json:"dynfield", because it will always be different

JSON

"title1": [ 
{
      "dynfield1" : {
        "field1" : "value1",
        "field2" : "value2",
         ...
         },
      "dynfield2" : {
        "field1" : "value1",
        "field2" : "value2",
         ...
         },
    }
]

“dynfield” can be any value

 type gomarsh struct {
 title1 []title1 `json:"title1"
 }
 
 type title1 struct {
 dynfield dynfield `json:"dynfieldn"` // As can be represented
}

If I understand correctly you want this:

type gomarhs struct {
  Title1 []map[string]title1 `json:"title1"`
}

type title1 struct {
  Field1 string `json:"field1"`
}

Yes, thank you, how I could forget about map in Go

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.