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 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. 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"`
    }
Sign up to request clarification or add additional context in comments.

4 Comments

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
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?
Yeah, I got the json from there too and tested it. Works fine.
Can I parse this json using a map in my struct for id and name. I tried using map to but didn't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.