All Questions
29 questions
2
votes
1
answer
385
views
Building a struct with a field name which has a comma
I'm trying to create a struct based on a response I get.
I have no control over the response, and in it's structure there are field names which use comma as part of the filed name itself
JSON Example:
...
2
votes
2
answers
2k
views
Ignoring an object in struct is nil and not when it's an empty array
Is it possible to only use omitempty when an object is nil and not when it's an empty array?
I would like for the JSON marshaller to not display the value when an object is nil, but show object: [] ...
-2
votes
1
answer
419
views
Encoding and decoding structs of
I'm trying to encode and decode structs, I've searched around quite a bit and a lot of the questions regarding this topic is usually people who want to encode primitives, or simple structs. What I ...
1
vote
2
answers
2k
views
Can I skip a json tag while Marshalling a struct in Golang?
I have a scenario where I would like to skip a json tag while marshalling a struct in Golang. Is that possible? If so how can I achieve this?
For example I am getting this json:
{"Employee":{...
-1
votes
2
answers
2k
views
How do you unmarshal a JSON object to a Golang struct when the JSON field key is a date?
I have the following JSON response. What would be the best way to unmarshal this into a Golang struct? The JSON to Golang autogenerated struct is saying the named properties of the struct should be ...
0
votes
2
answers
3k
views
How effectively to change JSON keys
At the input, the program receives a JSON.
I need to return the same JSON, but with camelCase keys.
Is there any effective way to convert all snake_case keys in JSON to camelCase keys in Go?
Snippet ...
-2
votes
1
answer
1k
views
How to Marshall any data type to string and Unmarshal from string to specific data type on condition
type State struct {
Type string `json:"type" validate:"required"`
Value string `json:"value"`
}
I have a struct like this. I need to pass the ...
2
votes
1
answer
3k
views
How to marshal nested struct to flat JSON
I am trying to marshall a nested struct with several of the same structs to a flat JSON structure E.G.
type A struct {
Value float64
Unit string
}
type B struct {
p1 string `json:p1`
...
-2
votes
1
answer
53
views
What Go tag should be used while defining struct of a JSON data if "field/key" for some values is varying or not known?
While trying to parse a time-series data I found a key field in the JSON data is the timestamp(obviously in string format). But creating a struct for the same beforehand is not possible as I cannot ...
1
vote
2
answers
4k
views
How to Marshall json from one struct to another with different json tags in Go?
I am creating a Go application that consumes data from multiple sources that all have similar data but different structures to their data/responses. These responses need to be marshalled into a common ...
3
votes
1
answer
2k
views
Unmarshall should return error when having wrong json input structure
I have struct A and B. When a JSON string A is unmarshalled to struct A then it is valid, however if the JSON string A is unmarshalled to struct B it is still successful (which should not).
Is there ...
1
vote
2
answers
790
views
Create heterogeneous json array in go
Suppose I have an struct like this in go:
type Message struct {
Args []interface{}
Kwargs map[string]interface{}
}
message := Message{ ...
32
votes
1
answer
17k
views
Why is Go json.Marshal rejecting these struct tags? What is proper syntax for json tags? [duplicate]
I am trying to use json.Marshal but it refuses to accept my struct tags.
What am I doing wrong?
Here is the source code for "marshal.go"
https://play.golang.org/p/eFe03_89Ly9
package main
import (...
2
votes
1
answer
6k
views
Golang unset Struct Field
In Golang, I have below Struct with three fields
type Person struct {
name string
age int
rank int
}
For Processing, I need a rank field, but for output, I want to exclude the rank field ...
2
votes
2
answers
2k
views
JSON Marshaling of composite structs which both implement MarshalJSON()
I faced recently the following problem and have not found any solution. I have two struct types in Go, let us call them Parent and Child. Child has an anonymous field of the type *Parent. However, ...
1
vote
1
answer
900
views
Marshal to JSON struct extended another struct with the same field label
I have 2 struct containing a field with the same label (id) and the same JSON annotation (`json:"id"`).
One struct (Bar) includes the fields labels and their values from the other struct (Foo).
I ...
0
votes
0
answers
2k
views
Custom JSON Marshal from Slice of Struct in Different Package
My goal is to create JSON marshal from a struct but with different fields.
For example, my struct have UID, Name, and Type fields, but I want to Marshal just 2 of them.
I have something like this:
...
3
votes
1
answer
1k
views
Marshal slice of structs as slice of numbers
I'm trying to figure out what is the best way to marshal to JSON string below structure:
type User struct {
Id string `json:"id"`
Roles []Role `json:"roles"`
}
type Role struct {
Id ...
0
votes
2
answers
900
views
unmarshalling arbitrary data
Is there a way to marshall JSON data in such a way that it can be unmarshalled in parts / sections?
Let's say that the top half of data is a "code" which would signal what to do with the ...
0
votes
1
answer
615
views
Golang: Multiple structs marshall issue: json format
For the following code, I get the error:
type A struct{
B_j []B `json:"A"`
}
type B struct
{
X string
Y string
}
func main() {
xmlFile, _ := os.Open("test.xml")
b, _ := ioutil....
1
vote
1
answer
3k
views
JSON and struct composition in Go
I have hierarchical structure similar to this Network -> Serie -> Season -> Episodes. This is a simplified version my real hierarchy is 7 levels deep.
I need to decode/encode these objects ...
-2
votes
2
answers
715
views
Unmarshalling JSON to custom format in Go
I am trying to get my json to be formatted in a particular way to be inserted into a database. I am able to unmarshal the json and map it to a struct just fine but I need to be able to add and remove ...
0
votes
2
answers
1k
views
Json unmarshal type model in golang
I have a RESTful service that returns response similar to show below:
"Basket" : {
"Count": 1,
"Fruits": {[
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
...
-2
votes
1
answer
438
views
json.Marshal behaves differently with two objects (Go/Golang)
So i want to marshal data to JSON. The basic struct looks like this:
type DatabaseObject struct {
Preferences []int `json:"preferences"`
Texts map[string]string `json:"texts"...
9
votes
2
answers
3k
views
Ordering Difference in JSON Marshaled from Map and Struct
When marshaling from a map[string]interface{} and an equivalent struct, the JSON returned are similar but different in the internal order of the keys likewise:
var arg1 = map[string]interface{}{
"...
3
votes
3
answers
468
views
How to embed a method result into JSON output when marshalling a type?
I'm seeking a clean approach to embed the return value of a method into the JSON marshalling of a type/value.
It would be great if I don't need to write custom JSON marshaller.
For example, if the ...
4
votes
1
answer
2k
views
Golang Struct Won't Marshal to JSON [duplicate]
I'm trying to marshal a struct in Go to JSON but it won't marshal and I can't understand why.
My struct definitions
type PodsCondensed struct {
pods []PodCondensed `json:"pods"`
}
func (p *...
11
votes
2
answers
4k
views
JSON marshalling/unmarshalling same struct to different JSON format in go?
I have a struct that I'd like to Marshal into JSON differently depending on the context.
For example, sometimes I want to marshal like this:
type MyStruct struct {
Nickname string `...
1
vote
1
answer
2k
views
Go map[int]struct JSON Marshal
Trying to parse a map[int] to user defined struct in go:
Here are the data schemas.
type Recommendation struct {
Book int `json:"book"`
Score float64 `json:"score"`
}
Here is the json ...