Dede
Dede

Reputation: 127

decoding nested json objects in go

I found some posts on how to decoding json nested objects in go, I tried to apply the answers to my problem, but I only managed to find a partial solution.

My json file look like this:

{
"user":{
    "gender":"male",
    "age":"21-30",
    "id":"80b1ea88-19d7-24e8-52cc-65cf6fb9b380"
    },
"trials":{
    "0":{"index":0,"word":"WORD 1","Time":3000,"keyboard":true,"train":true,"type":"A"},
    "1":{"index":1,"word":"WORD 2","Time":3000,"keyboard":true,"train":true,"type":"A"},
    },
"answers":{
    "training":[
        {"ans":0,"RT":null,"gtAns":"WORD 1","correct":0},
        {"ans":0,"RT":null,"gtAns":"WORD 2","correct":0}
        ],
    "test":[
        {"ans":0,"RT":null,"gtAns":true,"correct":0},
        {"ans":0,"RT":null,"gtAns":true,"correct":0}
        ]
    }
}

Basically I need to parse the information inside it and save them into go structure. With the code below I managed to extract the user information, but it looks too complicated to me and it won't be easy to apply the same thing to the "answers" fields which contains 2 arrays with more than 100 entries each. Here the code I'm using now:

type userDetails struct {
    Id     string `json:"id"`
    Age    string `json:"age"`
    Gender string `json:"gender"`
}

type jsonRawData map[string]interface {
}

func getJsonContent(r *http.Request) ( userDetails) {
    defer r.Body.Close()
    jsonBody, err := ioutil.ReadAll(r.Body)
    var userDataCurr userDetails
    if err != nil {
        log.Printf("Couldn't read request body: %s", err)
    } else {
        var f jsonRawData
        err := json.Unmarshal(jsonBody, &f)
        if err != nil {
            log.Printf("Error unmashalling: %s", err)
        } else {
            user := f["user"].(map[string]interface{})
            userDataCurr.Id = user["id"].(string)
            userDataCurr.Gender = user["gender"].(string)
            userDataCurr.Age = user["age"].(string)
        }
    }
    return userDataCurr
}

Any suggestions? Thanks a lot!

Upvotes: 0

Views: 902

Answers (1)

Dave C
Dave C

Reputation: 7878

You're doing it the hard way by using interface{} and not taking advantage of what encoding/json gives you.

I'd do it something like this (note I assumed there was an error with the type of the "gtAns" field and I made it a boolean, you don't give enough information to know what to do with the "RT" field):

package main

import (
        "encoding/json"
        "fmt"
        "io"
        "log"
        "strconv"
        "strings"
)

const input = `{
"user":{
    "gender":"male",
    "age":"21-30",
    "id":"80b1ea88-19d7-24e8-52cc-65cf6fb9b380"
    },
"trials":{
    "0":{"index":0,"word":"WORD 1","Time":3000,"keyboard":true,"train":true,"type":"A"},
    "1":{"index":1,"word":"WORD 2","Time":3000,"keyboard":true,"train":true,"type":"A"}
    },
"answers":{
    "training":[
        {"ans":0,"RT":null,"gtAns":true,"correct":0},
        {"ans":0,"RT":null,"gtAns":true,"correct":0}
        ],
    "test":[
        {"ans":0,"RT":null,"gtAns":true,"correct":0},
        {"ans":0,"RT":null,"gtAns":true,"correct":0}
        ]
    }
}`

type Whatever struct {
        User struct {
                Gender Gender   `json:"gender"`
                Age    Range    `json:"age"`
                ID     IDString `json:"id"`
        } `json:"user"`
        Trials map[string]struct {
                Index int    `json:"index"`
                Word  string `json:"word"`
                Time  int    // should this be a time.Duration?
                Train bool   `json:"train"`
                Type  string `json:"type"`
        } `json:"trials"`
        Answers map[string][]struct {
                Answer    int             `json:"ans"`
                RT        json.RawMessage // ??? what type is this
                GotAnswer bool            `json:"gtAns"`
                Correct   int             `json:"correct"`
        } `json:"answers"`
}

// Using some custom types to show custom marshalling:

type IDString string // TODO custom unmarshal and format/error checking

type Gender int

const (
        Male Gender = iota
        Female
)

func (g *Gender) UnmarshalJSON(b []byte) error {
        var s string
        err := json.Unmarshal(b, &s)
        if err != nil {
                return err
        }
        switch strings.ToLower(s) {
        case "male":
                *g = Male
        case "female":
                *g = Female
        default:
                return fmt.Errorf("invalid gender %q", s)
        }
        return nil
}
func (g Gender) MarshalJSON() ([]byte, error) {
        switch g {
        case Male:
                return []byte(`"male"`), nil
        case Female:
                return []byte(`"female"`), nil
        default:
                return nil, fmt.Errorf("invalid gender %v", g)
        }
}

type Range struct{ Min, Max int }

func (r *Range) UnmarshalJSON(b []byte) error {
        // XXX could be improved
        _, err := fmt.Sscanf(string(b), `"%d-%d"`, &r.Min, &r.Max)
        return err
}
func (r Range) MarshalJSON() ([]byte, error) {
        return []byte(fmt.Sprintf(`"%d-%d"`, r.Min, r.Max)), nil
        // Or:
        b := make([]byte, 0, 8)
        b = append(b, '"')
        b = strconv.AppendInt(b, int64(r.Min), 10)
        b = append(b, '-')
        b = strconv.AppendInt(b, int64(r.Max), 10)
        b = append(b, '"')
        return b, nil
}

func fromJSON(r io.Reader) (Whatever, error) {
        var x Whatever
        dec := json.NewDecoder(r)
        err := dec.Decode(&x)
        return x, err
}

func main() {
        // Use http.Get or whatever to get an io.Reader,
        // (e.g. response.Body).
        // For playground, substitute a fixed string
        r := strings.NewReader(input)

        // If you actually had a string or []byte:
        //     var x Whatever
        //     err := json.Unmarshal([]byte(input), &x)

        x, err := fromJSON(r)
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(x)
        fmt.Printf("%+v\n", x)

        b, err := json.MarshalIndent(x, "", "  ")
        if err != nil {
                log.Fatal(err)
        }
        fmt.Printf("Re-marshalled: %s\n", b)

}

Playground

Of course if you want to reuse those sub-types you could pull them out of the "Whatever" type into their own named types.

Also, note the use of a json.Decoder rather than reading in all the data ahead of time. Usually try and avoid any use of ioutil.ReadAll unless you really need all the data at once.

Upvotes: 3

Related Questions