I just recently started working with apis and http requests and I'm trying to build an application that uses the Reddit API to pull posts on a specific subreddit.
This is the the page with json and search parameters that I'm practicing on: https://www.reddit.com/r/hiphopheads.json?limit=1
Looking at the standard library of the JSON module for Golang, I still don't understand how to use json.Unmarshal for this complex JSON. From what I gather, I have to define a struct that resembles the JSON structure to actually hold the data
I posted the link into this website to get a feel for what the JSON is actually strucutred like: https://jsonformatter.curiousconcept.com/
Right now the main thing I'm after is the title which is under Data->Children->Data->Title. If I want to unmarshal the JSON into an object, do I define a nested struct object? Or is there a simpler way to do this so that I don't have to figure out all the attributes of the JSON and define them myself??
Any help that can get me on the right track is greatly appreciated. Thanks!
You don't have to define fields you don't need in your struct. Unmarshal will only decode the values that are present in your struct. But with nested JSONs you unfortunately have to define all the parent fields also (unlike in xml package in which you can define paths in tags). So your struct could look like this:
type Foo struct {
Data struct {
Children []struct {
Data struct {
Title string
}
}
}
}
See here for a working example: https://play.golang.org/p/UeUYfWBONL
It seems like the JSON you are trying to unmarshal is over complex, so your struct has to be complex, but that's the way it goes.
There are a few tools that generate struct definitions from a JSON, and that saves a ton of work. Using the JSON you posted and such an online tool, I generated the following struct:
package main
type MyJsonName struct {
Data struct {
After string `json:"after"`
Before interface{} `json:"before"`
Children []struct {
Data struct {
ApprovedBy interface{} `json:"approved_by"`
Archived bool `json:"archived"`
Author string `json:"author"`
AuthorFlairCSSClass string `json:"author_flair_css_class"`
AuthorFlairText interface{} `json:"author_flair_text"`
BannedBy interface{} `json:"banned_by"`
Clicked bool `json:"clicked"`
ContestMode bool `json:"contest_mode"`
Created int `json:"created"`
CreatedUtc int `json:"created_utc"`
Distinguished string `json:"distinguished"`
Domain string `json:"domain"`
Downs int `json:"downs"`
Edited bool `json:"edited"`
Gilded int `json:"gilded"`
Hidden bool `json:"hidden"`
HideScore bool `json:"hide_score"`
ID string `json:"id"`
IsSelf bool `json:"is_self"`
Likes interface{} `json:"likes"`
LinkFlairCSSClass string `json:"link_flair_css_class"`
LinkFlairText string `json:"link_flair_text"`
Locked bool `json:"locked"`
Media interface{} `json:"media"`
MediaEmbed struct{} `json:"media_embed"`
ModReports []interface{} `json:"mod_reports"`
Name string `json:"name"`
NumComments int `json:"num_comments"`
NumReports interface{} `json:"num_reports"`
Over18 bool `json:"over_18"`
Permalink string `json:"permalink"`
Quarantine bool `json:"quarantine"`
RemovalReason interface{} `json:"removal_reason"`
ReportReasons interface{} `json:"report_reasons"`
Saved bool `json:"saved"`
Score int `json:"score"`
SecureMedia interface{} `json:"secure_media"`
SecureMediaEmbed struct{} `json:"secure_media_embed"`
Selftext string `json:"selftext"`
SelftextHTML string `json:"selftext_html"`
Stickied bool `json:"stickied"`
Subreddit string `json:"subreddit"`
SubredditID string `json:"subreddit_id"`
SuggestedSort interface{} `json:"suggested_sort"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
Ups int `json:"ups"`
URL string `json:"url"`
UserReports []interface{} `json:"user_reports"`
Visited bool `json:"visited"`
} `json:"data"`
Kind string `json:"kind"`
} `json:"children"`
Modhash string `json:"modhash"`
} `json:"data"`
Kind string `json:"kind"`
}
Usually, the output of these tools still needs to manual tweaking to work properly. for example:
MediaEmbed struct{} `json:"media_embed"`
I'm pretty sure that's not what's needed here. But it does go a long way in showing the basic idea and figuring out most of the stuff correctly. There are other similar tools you can try.