I have a GoLang JSON Fetch, sending POST Request Question

Issue

I am receiving a JSON request. How do I parse it into individual variables so I can use them for SQL requests (and other things)?

I need "{‘name’:’jude’,’address’:’456 main’}" converted into a name variable and an address variable.

I got this far…

type Member struct {
       Name string `json:"name"`
       Address string `json:"address"`
}
   
jsonBody, _ := ioutil.ReadAll(req.Body)

But I can’t get the individual fields out of the json body.

Thank you in advance.

Solution

    member := &Member{}
    err := json.Unmarshal(jsonBody, member)

    fmt.Println(member.Name)
    fmt.Println(member.Address)
}

Try the example here: https://go.dev/play/p/2t2KEBiVRDF?v=goprev

Answered By – t3r

Answer Checked By – Cary Denson (GoLangFix Admin)

Leave a Reply

Your email address will not be published.