I'm trying to unmarshal this json https://www.reddit.com/r/videos/comments/3vgdsb/recruitment_2016.json
It starts as an array of two different objects, and I only need data on the second object.
I want to retrieve the comments body, it doesn't give me any error when i'm trying to decode it, but it doesn't capture the data I want.
This is the output I get from running this:
//Response struct when initialized: []
//Response struct decoded: [{{{[]}}} {{{[]}}}]
////
type Response []struct {
Parent struct {
Data struct {
Children []struct {
Com Comment
}
}
}
}
type Comment struct {
Name string `json:"body"`
}
func init() {
http.HandleFunc("/api/getcomments", getComments)
}
func getComments(w http.ResponseWriter, r *http.Request) {
url := "https://www.reddit.com/r/videos/comments/3vgdsb/recruitment_2016.json"
c := appengine.NewContext(r)
client := urlfetch.Client(c)
resp, err := client.Get(url)
if err != nil { fmt.Fprint(w, "Error client.Get(): ", err) }
re := new(Response)
fmt.Fprint(w, "Response struct: ", re, "\n")
errTwo := json.NewDecoder(resp.Body).Decode(&re)
if errTwo != nil { fmt.Fprint(w, "Error decoding: ", errTwo, "\n") }
fmt.Fprint(w, "Response struct: ", re)
}
For everyone that is struggling to create the right structure for JSON unmarshalling, here's a cool website that convert any JSON to the right Go struct: JSON-to-Go
The json data you are unmarshaling does not conform with your data, and if the names of the fields are not like in your struct, you should use struct tags as well. It should be more like this:
type Response []struct {
Kind string `json:"kind"`
Data struct {
Children []struct {
Data struct {
Replies []struct {
// whatever...
} `json:"replies"`
} `json:"data"`
} `json:"children"`
} `json:"data"`
}
}
Of course I'd replace the inline types with real, named types, but I'm just making a point here in regards to the data hierarchy.
Goddamn, that's some ugly bloated JSON BTW.
Related
I'm having a hard time unmarshalling a struct that is sent to me from an AWS lambda function.
All of the pkidata fields are empty no matter what I do. I created this code sample to isolate the process :
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
)
func main() {
mystring := `{"error":{"errormessage": "errormessagetest", "errortype": "errortypetest"}, "pkidata":{"certificate":"Q2VydGlmaWNhdGU6CiAgICBEYXRhOgogICAgICAgIFZlcnNpb246IDMgKDB4MikKICAgICAgICBTZXJpYWwgTnVtYmVyOgogICAgICAgICAgICBhYTo5MzpiMTo4MDphYjpiYTplYzo5ZTo4MzpmOTo2OTo5NToyNDpkZjo5Njo2OQogICAgU2lnbmF0dXJlIEFsZ29yaXRobTogc2hhMjU2V2l0aFJTQUVuY3J5cHRpb24KICAgICAgICBJc3N1ZXI6IENOPXJhcGhvZXN0ZXIKICAgICAgICBWYWxpZGl0eQogICAgICAgICAgICBOb3QgQmVmb3JlOiBNYXIgMjcgMTk6NDM6MTQgMjAyMiBHTVQKICAgICAgICAgICAgTm90IEFmdGVyIDogSnVuIDI5IDE5OjQzOjE0IDIwMjQgR01UCiAgICAgICAgU3ViamVjdDogQ049cmFwaG9lc3RlcgogICAgICAgIFN1YmplY3QgUHVibGljIEtleSBJbmZvOgogICAgICAgICAgICBQdWJsaWMgS2V5IEFsZ29yaXRobTogcnNhRW5jcnlwdGlvbgogICAgICAgICAgICAgICAgUHVibGljLUtleTogKDIwNDggYml0KQogICAgICAgICAgICAgICAgTW9kdWx1czoKICAgICAgICAgICAgICAgICAgICAwMDpjNjplZjpkNjoxYTo0Mjo0Mjo2ZTo5MTpiODpmNTpiZjoyNToyNTpkODoKICAgICAgICAgICAgICAgICAgICBhNjo4Yjo4MzpmYjo2ZDo4MzpiNjowZTo3Mzo4ZjowYTozMjo5ZjpjNjplMjoKICAgICAgICAgICAgICAgICAgICBmMTo4Njo5NDoxMzo5MjplYjo0MTozNTo4NTpiZDo0ZDpkNzowNzo4ZTo5YjoKICAgICAgICAgICAgICAgICAgICBjYzoyYzpkMjo2Nzo5ZDpjOTo1Mzo4Yjo4ODpjZToxMTozOTpkODpkZTo3MjoKICAgICAgICAgICAgICAgICAgICBhMTphODo4MDozMDpmOTpiYTpiOTo3NTpjNTphMjoxYjpiZToyMDo3Yzo1ZjoKICAgICAgICAgICAgICAgICAgICBhMjpjZjpiNzphMjo2ODo4MzpiMzoxMTplODoyNTo3Mzo2MzoyNjphNzpiODoKICAgICAgICAgICAgICAgICAgICBjMzpjZDpjODplZTozNjphZjo4ZTo4ZjowZTo5NDowMToyMDpmZjowZTo0MToKICAgICAgICAgICAgICAgICAgICBjYjplZDpmYjo3NDowNToyZDo0NDphOToxNjoxZDo0ODpkNDphNDo3NTo0ZjoKICAgICAgICAgICAgICAgICAgICBlZjo0NDozYTphMDpmMDo1ZTo5ZTo5YjoxOTphZTo4MTozMzpkMDpkMTpmYzoKICAgICAgICAgICAgICAgICAgICA0MTphMTo1OTo5Zjo3MDoxZDphYTo3MzoyNTpiNTpiNjo3NDo2Mzo1NzpjZjoKICAgICAgICAgICAgICAgICAgICAzNTplNzpiZToyNjo1MzoxNDpkNTplNzpiMzpmYToyZDpjYzo5NToyMzo0ODoKICAgICAgICAgICAgICAgICAgICBkOTpkNTozNDplMjowNzoxYjoyNzoyMzpiZTpiODpmNzpiNTpkOTo3MDo1ZDoKICAgICAgICAgICAgICAgICAgICAyOTo4NTo3NToyYjpmYTo0MjpmNTo2Mjo1OTozZjowODo3Njo0Nzo2NjpmNzoKICAgICAgICAgICAgICAgICAgICAxYzpiYzozNzo1MjpiMDo3ODoyNzozNDo3MTo3ZTpiNjozZDowNDpjZDoyMzoKICAgICAgICAgICAgICAgICAgICAzMjphNjoxYTo2NzphNDo4ODoyYTowYTpmMjoxMjphNTo3ODpkYjpiZDpjOToKICAgICAgICAgICAgICAgICAgICA1ZToxMTo4Njo0MDpkMTo0MTplZjpkYzo2Yjo4Zjo1NjpmZDo1MjpkYTo0ZjoKICAgICAgICAgICAgICAgICAgICBiNTpmMDozYTpmZTpjNzoxMDo3ZTpjNDo3OTo2MTo1ZTpkYjo5Zjo4NjpjOToKICAgICAgICAgICAgICAgICAgICBmYjpiNwogICAgICAgICAgICAgICAgRXhwb25lbnQ6IDY1NTM3ICgweDEwMDAxKQogICAgICAgIFg1MDl2MyBleHRlbnNpb25zOgogICAgICAgICAgICBYNTA5djMgQmFzaWMgQ29uc3RyYWludHM6IAogICAgICAgICAgICAgICAgQ0E6RkFMU0UKICAgICAgICAgICA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WXJJWXppcVg5M015UzNSUCtrYXAvV0dIYlZDcWtpZ2lTODZwQkY1NDh3OWxXQ0xJai9kTgozRTI2V24zNgotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg=="}}`
// yeah the string is long lol
var body ResponseBody
if err := json.Unmarshal([]byte(mystring), &body); err != nil {
fmt.Println(fmt.Errorf("failed unmarshaling response | %s", err.Error()))
}
fmt.Println(body.Error.ErrorMessage)
certChain, err := base64.StdEncoding.DecodeString(mystring)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(certChain)
}
type PkiGenerationInput struct {
TeacherUsername string `json:"teacherusername"`
}
type Response struct {
Headers ResponseHeaders `json:"headers"`
Body ResponseBody `json:"body"`
}
type ResponseHeaders struct {
ContentType string `json:"Content-Type"`
}
type ResponseBody struct {
PkiData ParsablePkiData `json:"pkidata"`
Error struct {
ErrorMessage string `json:"errormessage"`
ErrorType string `json:"errortype"`
} `json:"error"`
}
type ParsablePkiData struct {
Certificate string `json:"certificate"`
PrivateKey string `json:"privatekey"`
CertificateChain string `json:"certificatechain"`
}
Here is the output
errormessagetest
illegal base64 data at input byte 0
[]
the fact I can read the errormessagetest shows that the unmarshalling kinda works, so that's weird.
I checked for any unexported fields but we can clearly see they are all capitalized. I also reduced the string size "just to see" and it didn't change anything.
Answer to synthetize all of the comments :
First of all, my "dumb mistake" was because I tried to decode the wrong variable :
// mystring needs to be replaced by body.PkiData.Certificate
base64.StdEncoding.DecodeString(mystring)
But the research led us to discover that the json.Unmarshal() function decodes base64 by itself when the result type matches what the declared type. Given the fact the encoded data was a []byte, I just replaced the string type by a []byte in the struct.
That allows to create a much more simple version of the previous code :
type ResponseBody struct {
PkiData PkiData `json:"pkidata"`
}
type PkiData struct {
Certificate []byte
PrivateKey []byte
CertificateChain []byte
}
var body ResponseBody
if err := json.Unmarshal(rawResp.Payload, &body); err != nil {
return PkiData{}, fmt.Errorf("failed unmarshaling response | %s", err.Error())
}
I am trying to process a song using API.I have tried using a specific URL in http.get and further unmarshalling the data but the only element returned in the console is {}. Any help to send me in the right direction is appreciated.
Edit: here is some code. I have this in my main file.
var data [2]Data
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatal("Didn't get 200")
}
rawData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(rawData, &data)
fmt.Println(data[0])
I have a struct created in a separate file for JSON tags/keys.
type Data struct {
SongID string `json:id`
Name string `json:name`
}
type data struct {
Error bool `json:"error"`
Response struct {
Results []struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"results"`
} `json:"response"`
}
This should be the data structure.
Use curl to request
curl https://searchly.asuarez.dev/api/v1/song/search?query=hello
use https://mholt.github.io/json-to-go/ to convert the JSON response to Golang struct.
I'm reading a file.json into memory. It's an array of objects, sample:
[
{"id":123123,"language":"ja-JP","location":"Osaka"}
,{"id":33332,"language":"ja-JP","location":"Tokyo"}
,{"id":31231313,"language":"ja-JP","location":"Kobe"}
]
I want to manipulate certain keys in this JSON file, so that they start with uppercase. Meaning
"language" becomes "Language" each time it's found. What I've done so far is to make a struct representing each object, as such:
type sampleStruct struct {
ID int `json:"id"`
Language string `json:"Language"`
Location string `json:"Location"`
}
Here, I define the capitalization. Meaning, id shouldn't be capitalized, but location and language should.
Rest of the code is as such:
func main() {
if len(os.Args) < 2 {
fmt.Println("Missing filename parameter.")
return
}
translationfile, err := ioutil.ReadFile(os.Args[1])
fileIsValid := isValidJSON(string(translationfile))
if !fileIsValid {
fmt.Println("Invalid JSON format for: ", os.Args[1])
return
}
if err != nil {
fmt.Println("Can't read file: ", os.Args[1])
panic(err)
}
}
func isValidJSON(str string) bool {
var js json.RawMessage
return json.Unmarshal([]byte(str), &js) == nil
}
// I'm unsure how to iterate through the JSON objects and only uppercase the objects matched in my struct here.
func upperCaseSpecificKeys()
// ...
Desired output, assuming the struct represents the whole data object, transform each key as desired:
[
{"id":123123,"Language":"ja-JP","Location":"Osaka"}
,{"id":33332,"Language":"ja-JP","Location":"Tokyo"}
,{"id":31231313,"Language":"ja-JP","Location":"Kobe"}
]
The documentation on json.Unmarshal says (with added emphasis):
To unmarshal JSON into a struct, Unmarshal matches incoming object
keys to the keys used by Marshal (either the struct field name or its
tag), preferring an exact match but also accepting a case-insensitive
match
See example here: https://play.golang.org/p/1vv8PaQUOfg
One way is to implement custom marshal method, although not very flexible:
type upStruct struct {
ID int `json:"id"`
Language string
Location string
}
type myStruct struct {
ID int `json:"id"`
Language string `json:"language"`
Location string `json:"location"`
}
func (m myStruct) MarshalJSON() ([]byte, error) {
return json.Marshal(upStruct(m))
}
....
func main() {
var mySArr []myStruct
// 1. Unmarshal the input
err := json.Unmarshal([]byte(myJson), &mySArr)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Input: \n%+v\n", mySArr)
// 2. Then, marshal it using our custom marshal method
val, err := json.Marshal(mySArr)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Output: \n%v\n", string(val))
}
Link to working code: https://play.golang.org/p/T4twqPc34k0
Thanks to mkopriva
I am trying to parse the following json structure, where the fields marked with "val1" and "val2" are constantly changing, so I cannot use a predefined struct. How could I parse this json in a way to be able to loop through every single "val"? Thank you!
{"result":true,"info":{"funds":{"borrow":{"val1":"0","val2":"0"},"free":{"val1":"0","val2":"0"},"freezed":{"val1":"0","val2":"0"}}}}
By unmarshalling into the following struct I can loop through the desired fields.
type Fields struct {
Result bool `json:"result"`
Info struct {
Funds struct {
Borrow, Free, Freezed map[string]interface{}
} `json:"funds"`
} `json:"info"`
}
package main
import (
"fmt"
"encoding/json"
)
type Root struct {
Result bool `json:"result"`
Info Info `json:"info"`
}
type Info struct {
Funds struct {
Borrow, Free, Freezed map[string]interface{}
} `json:"funds"`
}
func main() {
var rootObject Root
jsonContent := " {\"result\":true,\"info\":{\"funds\":{\"borrow\":{\"val1\":\"0\",\"val2\":\"0\"},\"free\":{\"val1\":\"0\",\"val2\":\"0\"},\"freezed\":{\"val1\":\"0\",\"val2\":\"0\"}}}}"
if err := json.Unmarshal([]byte(jsonContent), &rootObject); err != nil {
panic(err)
}
fmt.Println(rootObject)
}
I have the following code to try to Unmarshal this json file, however the line json.Unmarshal([]byte(msg["restaurant"]), &restaurant) always gives an error. How can I make Unmarshal ignore the "restaurant" or pass only the "restaurant" data to the Unmarshal function?
Thanks!
{
"restaurant": {
"name": "Tickets",
"owner": {
"name": "Ferran"
}
}
}
file, e := ioutil.ReadFile("./rest_read.json")
if e != nil {
fmt.Println("file error")
os.Exit(1)
}
var data interface{}
json.Unmarshal(file, &data)
msg := data.(map[string]interface{})
log.Println(msg)
log.Println(msg["restaurant"])
log.Println(reflect.TypeOf(msg["restaurant"]))
var restaurant Restaurant
json.Unmarshal([]byte(msg["restaurant"]), &restaurant)
log.Println("RName: ", restaurant.Name)
log.Println("Name: ", restaurant.Owner.Name)
It is possible to do generic unmarshalling ala gson by decoding into an interface and then extracting a top level map from the result, e.g:
var msgMapTemplate interface{}
err := json.Unmarshal([]byte(t.ResponseBody), &msgMapTemplate)
t.AssertEqual(err, nil)
msgMap := msgMapTemplate.(map[string]interface{})
See "decoding arbitrary data" in http://blog.golang.org/json-and-go for more into.
I would propose to construct a proper model for your data. This will enable you to cleanly unmarshal your data into a Go struct.
package main
import (
"encoding/json"
"fmt"
)
type Restaurant struct {
Restaurant RestaurantData `json:"restaurant"`
}
type RestaurantData struct {
Name string `json:"name"`
Owner Owner `json:"owner"`
}
type Owner struct {
Name string `json:"name"`
}
func main() {
data := `{"restaurant":{"name":"Tickets","owner":{"name":"Ferran"}}}`
r := Restaurant{}
json.Unmarshal([]byte(data), &r)
fmt.Printf("%+v", r)
}
Unmarshalling occurs recursively, so msg["restaurant"] is no longer a json string - it is another map[string]interface{}. If you want to unmarshall directly into a Restaurant object, you will have to provide a simple wrapper object with a Restaurant member and unmarshall into that.