Andrew Andrew - 4 months ago 26
JSON Question

Unmarshal JSON with unknown field name

I have a JSON object, something like this:

"randomstring": {
"everything": "here",
"is": "known"

Basically everything inside the
object is known, I can model that, but the randomstring itself is random. I know what it's going to be, but it's different every time. Basically all the data I need is in the randomstring object. How could I parse this kind of JSON to get the data?


Use a map where the key type is string and the value type is a struct with the fields you want, like in this example on the Playground and below:

package main

import (

type Item struct{ X int }

var x = []byte(`{
    "zbqnx": {"x": 3}

func main() {
    m := map[string]Item{}
    err := json.Unmarshal(x, &m)
    if err != nil {