0

I cannot parse this json into my structure. Can anyone please help with this

{"error":false,"response":{"results":[{"id":68876,"name":"cee lo green - big girls"},{"id":68954,"name":"charles, ray - the girl friend"},{"id":69603,"name":"charlie puth - la girls"},{"id":68001,"name":"city girls - careless"},{"id":68000,"name":"city girls - millionaire dick"},{"id":68002,"name":"city girls - period (we live)"},{"id":68004,"name":"city girls - rap shit"},{"id":68003,"name":"city girls - runnin"},{"id":68019,"name":"clairo - pretty girl"},{"id":68223,"name":"cohn, marc - girl of mysterious sorrow"},{"id":68343,"name":"contours, the - searching for a girl"}

Below is my struct package main

import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http")

type test struct{ SngID string json:"id" SngNm string json:"name" } type Inner struct{

Result[10] test `json:"results"`

}

type Outer struct{ Eror bool json:"error" Response [] Inner json:"results"

}

2
  • I tried to mimic the my data structure to the structure of json. But the json data is not parsing into my struct. Below is my Code
    – Surbhi
    Commented Sep 26, 2020 at 0:56
  • I added my code to the my question. Can you please suggest something what mistake i am doing here.
    – Surbhi
    Commented Sep 26, 2020 at 1:03

1 Answer 1

2

Your JSON is malformed (check the error returned by json.Unmarshal).

Anyway, this struct should work for you.

    type Response struct {
        Error    bool `json:"error"`
        Response struct {
            Results []struct {
                ID   int    `json:"id"`
                Name string `json:"name"`
            } `json:"results"`
        } `json:"response"`
    }
4
  • Thankyou for the Response. I tried the struct you gave. Its still not parsing values for id and name My output looks like this: Just parsing bool value for error [{Error:false Response:{Results:[{ID:0 Name:} {ID:0 Name:} {ID:0 Name:} {ID:0 Name:} {ID:0 Name:}]}}] Process finished with exit code 0
    – Surbhi
    Commented Sep 26, 2020 at 1:45
  • I tested it with the json you gave and it works: play.golang.org/p/-ccZbeEDtH-. Could you provide a playground reproduction of the error?
    – user14311453
    Commented Sep 26, 2020 at 1:49
  • Yeah, I got the json from there too and tested it. Works fine.
    – user14311453
    Commented Sep 26, 2020 at 2:02
  • Can I parse this json using a map in my struct for id and name. I tried using map to but didn't work.
    – Surbhi
    Commented Sep 27, 2020 at 1:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.