Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to decode or unmarshal embedded JSON in Golang?

This is the sample in the Playground. The gist of the problem is that I am unable to decode / unmarshal this

    Name := "TestName"
    Desc := "Test Desc"
    Body := []byte(`{"key": "value"}`)//simplest possible JSON but will have multiple levels

    requestJson := fmt.Sprintf(`{"name": "%s","description": "%s","body": "%s"}`, Name, Desc, Body)
    decoder := json.NewDecoder(strings.NewReader(requestJson))
    err := decoder.Decode(&createRpt)
    if err != nil {
        fmt.Println("Report body is expected to be valid JSON", "error", err)
        return
    }

I have also tried to use the unmarshal option as can be seen in the playground .

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have a typo in your json, remove the " around body’s value and it will be fixed:

requestJson := fmt.Sprintf(`{"name": "%s","description": "%s","body": %s}`, Name, Desc, Body)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading