Golang Json Marshall encoding synthax [duplicate] - json

This question already has answers here:
Lowercase JSON key names with JSON Marshal in Go
(4 answers)
Closed 7 years ago.
i would know if json.Marshal Put Uppercase a the first letter of each name fields ? I need to encode some data with just a lowercase at each field name first letter.
just:
{
"name":"thomas"
}
instead of:
{
"Name":"thomas"
}
Thanks !

You have to add an annotation to the Name field.
Supposing your struct is:
type User struct {
Name string
}
You have to change it to:
type User struct {
Name string `json:"name"`
}
The json marshaller will replace Name with name

Related

How to represent Golang struct with variable field names [duplicate]

This question already has answers here:
How to Unmarshal jSON with dynamic key which can't be captured as a `json` in struct: GOlang [duplicate]
(1 answer)
How to parse/deserialize dynamic JSON
(4 answers)
Closed 8 months ago.
If this has been answered somewhere else let me know but I couldn't find anything.
Basically, let's say we have the following JSON. string-id-001 can be an arbitrary string. We want to unmarshal it into a struct, and be able to access the unique id's.
{"list":{"string-id-001":{"id":"blah","name":"cool"},"string-id-002":{"id":"yas","name":"rad"}}}
Golang as far as I can tell would require something like below which doesn't work if the keyhere value is constantly changing. Eg if it's an ID
type Foo struct {
List struct {
StringID001 struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"string-id-001"`
StringID002 struct {
ID string `json:"id"`
Name string `json:"name"`
} `json:"string-id-002"`
} `json:"list"`
}
I've seen a similar issue in another project (which I solved with interfaces rather than structs), and I'm wondering if there's a nicer solution. Am I missing something obvious?
type payLoad struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Foo struct {
List map[string]payLoad `json:"list"`
}

json marshalling of a complex structure in Golang [duplicate]

This question already has answers here:
Serialize a mixed type JSON array in Go
(3 answers)
Golang decoding JSON into slice with one string and one float64
(1 answer)
Fixed-size array that contains multiple specific types?
(1 answer)
Closed 3 years ago.
I just started developing in Golang, and am up against this problem:
I have json data to be accepted by a Golang server. A sample of one of the fields of this json data is below:
"DATA":[["(610, 658)",1573824148],["(594, 675)",1573824148],["(578,710)",1573824148],["(571, 728)",1573824148],["(552, 769)",1573824148],["(549, 788)",1573824148],["(549, 796)",1573824148]]
Note that it is an array of lists, each list being a string and an integer.
I have the following data structures declared:
type POINT struct {
p string
t int
}
type POINTS struct {
A int
B int
C string
DATA []FIDGET
}
The relevant field is POINTS.DATA which is an array of POINTs. A POINT is a string and an int. To convert from json to Go variables, I use the encoding/json package.
My problem is, when I call
err = json.Unmarshal(body, &m)
I get this error:
json: cannot unmarshal array into Go struct field POINTS.DATA of type main.POINT
Can anyone help me? Thank you in advance!

unmarshall json using dynamic key [duplicate]

This question already has answers here:
Golang parse a json with DYNAMIC key [duplicate]
(1 answer)
How to parse/deserialize dynamic JSON
(4 answers)
Marshal dynamic JSON field tags in Go
(1 answer)
How to Unmarshal jSON with dynamic key which can't be captured as a `json` in struct: GOlang [duplicate]
(1 answer)
Closed 4 years ago.
I'm receiving a json object that has a known-static structure inside a key that varies between 10 different values.
Consider lastname can be any in a list of 10 lastnames:
var lastnames = [...]string { "Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia", "Rodriguez", "Wilson" }
Now, this is how the json looks:
{
(lastname here):
{
"position": value,
"user_email": value
}
}
I tried to unmarshall it using the following structs, but I only get null values:
type Inside struct {
Rol string `json:"position"`
Email string `json:"user_email"`
}
type Outside struct {
Key Inside
}
...
var outside Outside
json.Unmarshal([]byte(body), &outside)
Is it possible to unmarshall this directly without creating 10 different structs? Is there possible workaround?

How to deserialize list with mixed types? [duplicate]

This question already has answers here:
Unmarshal 2 different structs in a slice
(3 answers)
Closed 4 years ago.
How would I deserialize this JSON in Go?
{
"using": [ "jmap-core", "jmap-mail" ],
"methodCalls": [
["method1", {"arg1": "arg1data", "arg2": "arg2data"}, "#1"],
["method2", {"arg1": "arg1data"}, "#2"],
["method3", {}, "#3"]
]
}
I haven't figured out how to properly get the json module to parse the methodCalls into a type. My first idea was
type MethodCall struct {
Name string
Params map[string]string
ClientId string
}
and then to use it as a list type:
type Request struct {
Using []string
MethodCalls []MethodCall
}
But this does not work. :using" is correctly parsed, but the "methocCalls" are not. Is there a way to get Go to parse this JSON into my types?
It looks like the methodCalls that you are trying to deserialize it is an array of strings instead of a struct for MethodCall.
So, Take a look at this link that I am deserializing as an array.
If you want to use the MethodCall struct you have to change the json a little bit. Take a look at this link

Struct for JSON objects in GO [duplicate]

This question already has an answer here:
Strange type definition syntax in Golang (name, then type, then string literal)
(1 answer)
Closed 7 years ago.
I'm learning GO and when defining structs for working with JSON like below.
type List struct {
ID string `datastore:"-"`
Name string
}
I see that there is this text in between ` sign. I have not been able to find an explanation what that signifies.
Things seem to work even without those.
They are struct tags used in Marshal'ing Go struct into JSON. In JSON, unlike Go, fields are in lowercase strings. Therefore, most use cases would be
type List struct {
ID string `json:"id"`
Name string `json:"name"`
}
In JSON
{
"id": "some id",
"name": "some name"
}
See post here