This question already has answers here:
json.Marshal(struct) returns "{}"
(3 answers)
JSON and dealing with unexported fields
(2 answers)
(un)marshalling json golang not working
(3 answers)
Parsing JSON in Golang doesn't Populate Object [duplicate]
(1 answer)
Printing Empty Json as a result [duplicate]
(1 answer)
Closed 2 months ago.
I have json which is not decoded to struct.
I know that error somewhere in my code, but I'm stuck and do not know where the error is and what am I doing wrong
Help me please, here is my code:
type GOOGLE_JSON struct {
code string `json:"code"`
clientId string `json:"clientId"`
redirectUri string `json:"redirectUri"`
}
body := []byte(`{"code":"111","clientId":"222","redirectUri":"333"}`)
//google_json := GOOGLE_JSON{}
var google_json GOOGLE_JSON
err := json.Unmarshal(body, &google_json)
if err != nil {
fmt.Println(err)
}
fmt.Println(google_json)
example here
I've found error
was
type GOOGLE_JSON struct {
code string `json:"code"`
clientId string `json:"clientId"`
redirectUri string `json:"redirectUri"`
}
must be capital letters
type GOOGLE_JSON struct {
Code string `json:"code"`
ClientId string `json:"clientId"`
RedirectUri string `json:"redirectUri"`
}
I was inattentive
Code // <- exported
ClientId // <- exported
RedirectUri // <- exported
code // <-- not exported
clientId // <-- not exported
redirectUri // <-- not exported
Related
This question already has answers here:
json.Unmarshal returning blank structure
(1 answer)
Making HTTP responses with JSON [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to parse a json file using GoLang but it seems like not work. Am I doing right?
this is my Go Code:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
)
type info struct {
username string `json:"username"`
password string `json:"password"`
timedelay int `json:"timedelay"`
courselist []string `json:"courselist"`
}
func main() {
var jsonParse info
file, err := ioutil.ReadFile("info.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("My file content is:\n", string(file))
fmt.Println("---------------Parsing Json File----------------")
json.Unmarshal(file, &jsonParse)
fmt.Println("My Json Parse is:\n", jsonParse)
}
and this is my json file
{
"username" : "17020726",
"password": "Blueflower",
"timedelay": 200,
"courselist": ["54"]
}
this is result of my code
My file content is:
{
"username" : "17020726",
"password": "Blueflower",
"timedelay": 200,
"courselist": ["54"]
}
---------------Parsing Json File----------------
My Json Parse is:
{ 0 []}
The info struct fields are private. Capitalize the first letters.
This question already has an answer here:
Printing Empty Json as a result [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to learn Go. I am writing a simple program to get values from JSON file in GoLang.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
type bands struct {
id string `json:"id"`
name string `json:"name"`
location string `json:"location"`
year string `json:"year"`
}
func main() {
bands := getBands()
fmt.Println(bands)
}
func getBands() []bands {
raw, err := ioutil.ReadFile("../data/bands.json")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var c []bands
json.Unmarshal(raw, &c)
return c
}
Also, below is my JSON File:
[{"id":"1","name": "The Beatles","location": "NY","year": "2012"},
{"id":"2","name": "Nirvana","location": "NY","year": "2010"},
{"id":"3","name": "Metallica","location": "NY","year": "1980"}]
When i am running the file, I am getting blank values.
Thanks for the help.
The fields must start with uppercase letters.
type bands struct {
Id string `json:"id"`
Name string `json:"name"`
Location string `json:"location"`
Year string `json:"year"`
}
This question already has answers here:
How to parse/deserialize dynamic JSON
(4 answers)
Closed 3 years ago.
I have this struct defined:
type X struct {
A string `json:"a_known_string"`
B string `json:"b_known_string"`
}
This sample JSON:
jsnStr := [read in from a file and printed out to confirm]
It is:
{
"any string" : {
"a_known_string" : "some value",
"b_known_string" : "another value"
}
}
If it was just the struct, I could:
var x X
err := json.Unmarshal(jsnStr, &x)
But I need to capture that 'any string'.
How do I do that please?
Use a map:
var m map[string]X
err := json.Unmarshal([]byte(jsnStr), &m)
playground example
This question already has an answer here:
How Do I Parse a JSON file into a struct with Go
(1 answer)
Closed 7 years ago.
I cannot get the standard library's encoding/json package to work for decoding JSON objects. Here's a minimal example:
b := []byte(`{"groups":[{"name":"foo"},{"name":"bar"}]}`)
type Group struct{ name string }
var contents struct {
groups []Group
}
err := json.Unmarshal(b, &contents)
fmt.Printf("contents = %+v\nerr = %+v\n", contents, err)
This prints:
contents = {groups:[]}
err = nil
But I expect:
contents = {groups:[{name:foo} {name:bar}]}
What am I doing wrong?
The field names have to start with a capital letter:
type Group struct{ Name string }
var contents struct {
Groups []Group
}
This question already has answers here:
JSON and dealing with unexported fields
(2 answers)
Printing Empty Json as a result [duplicate]
(1 answer)
(un)marshalling json golang not working
(3 answers)
json.Marshal(struct) returns "{}"
(3 answers)
Closed 10 months ago.
As part of an Oauth application, I need to decode some JSON. But I cannot get the object populated. There is no failure, but the data just isn't there. I've tried a bunch of different ways...
I have recreated the problem at http://play.golang.org/p/QGkcl61cmv
import (
"encoding/json"
"fmt"
"strings"
)
type RefreshTokenData struct {
id string `json:"id"`
issued_at string `json:"issued_at"`
scope string `json:"scope"`
instance_url string `json:"instance_url"`
token_type string `json:"token_type"`
refresh_token string `json:"refresh_token"`
signature string `json:"signature"`
access_token string `json:"access_token"`
}
func main() {
var tokenResp = `
{"id":"https://google.com","issued_at":"1423698767063",
"scope":"full refresh_token",
"instance_url":"https://na15.salesforce.com",
"token_type":"Bearer",
"refresh_token":"2os53__CCU5JX_yZXE",
"id_token":"5jSH0Oqm7Q4fc0xkE9NOvW8cA13U",
"signature":"/599EkGVIBsKPFRNkg+58wZ3Q7AFyclvIGvCrxVeyTo=",
"access_token":"sadfasdfasdfasdfdsa"}`
var tokenData RefreshTokenData
decoder := json.NewDecoder(strings.NewReader(tokenResp))
if jsonerr := decoder.Decode(&tokenData); jsonerr != nil {
fmt.Println("****Failed to decode json")
} else {
fmt.Println("****Refresh token: " + tokenData.refresh_token)
}
}
The JSON encoding package works with exported fields only. Capitalize the field names to export them:
type RefreshTokenData struct {
Id string `json:"id"`
Issued_at string `json:"issued_at"`
Scope string `json:"scope"`
Instance_url string `json:"instance_url"`
Token_type string `json:"token_type"`
Refresh_token string `json:"refresh_token"`
Signature string `json:"signature"`
Access_token string `json:"access_token"`
}
playground example