How to Convert Json String to Struct

I have start working in golang, I am trying to parse JSON string to struct but its not working.

JSON String:

dailies":[{"userAccessToken":"acessToken","uploadStartTimeInSeconds":1499744832,"uploadEndTimeInSeconds":1499744832,"callbackURL":"callbackurl"}]}

type pingDataFormat struct {
    userAccessToken          string
    uploadStartTimeInSeconds int
    uploadEndTimeInSeconds   int
    callbackURL              string 
}

Below is code which convert JSON String to structs

pingJSON := make(map[string][]pingDataFormat)

err := json.Unmarshal([]byte(pingData), &pingJSON)

if err != nil {
    panic(err)
}

fmt.Printf("\n\n json object:::: %v", pingJSON)

output is:

json object:::: map[dailies:[{ 0 0 }]]

Here "dailies" dont have data, So do i following right approach to parse and how to get data after parse?

1 Answer

The fields in pingDataFormat need to be exported (start with a capital letter)

type pingDataFormat struct {
    UserAccessToken          string `json:"userAccessToken"`
    UploadStartTimeInSeconds int    `json:"uploadStartTimeInSeconds"`
    UploadEndTimeInSeconds   int    `json:"uploadEndTimeInSeconds"`
    CallbackURL              string `json:"callbackURL"`
}

That should solve your problem

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest