How to unmarshal multi dimensional array inside struct - arrays

I have following data:-
{"me":[{"id": "0xcfd","Title":"Story of Stackoverflow","Users":[{"id":"1","Name":"MetaBoss"},{"id":"2","Name":"Owner"}],"Tag":"golang,programming"}]}
and I have the following struct:-
type Root struct {
ID string `json:"id,omitempty"`
Title string `json:"Title,omitempty"`
Myuser Users `json:"Users,omitempty"` // Users is struct
Tag string `json:"Tag,omitempty"`
}
type Users struct {
ID string `json:"id,omitempty"`
Name string `json:"Name,omitempty"`
}
To unmarshal the data, I am trying to do following things -
type Unmarh struct {
Me []Root `json:"me"`
}
var r Unmarh
err = json.Unmarshal(response, &r)
while printing r.Me[0].Myuser, I am not able to get data.
I am getting below error -
json: cannot unmarshal array into go struct field Root.Myuser of type User struct {....Users struct data}
It needs Myuser to be multidimensional array type and not Users struct. I have no Idea, how to represent Users multidimensional array inside struct

In the json the Users key is an array and so the corresponding Go field should be a slice.
type Root struct {
ID string `json:"id,omitempty"`
Title string `json:"Title,omitempty"`
Users []User `json:"Users,omitempty"`
Tag string `json:"Tag,omitempty"`
}
https://play.golang.org/p/azE7kPFs02V

Related

How do I Change the Position of Golang Struct Values?

How would I change the position of the json values?
What Im trying to achieve:
[{"key":"f","value":"f"},{"value":"f","key":"f"}]
Problem:
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
}
func main() {
test := []Struct{ {Key: "test",Value: "wep"}, {Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
Running this code prints [{"key":"test","value":"wep"},{"key":"test","value":"wep"}]
I have also tried doing something like this but it just printed empty values
type Struct struct {
Key string `json:"key"`
Value string `json:"value"`
Value2 string `json:"value"`
Key2 string `json:"key"`
}
But how would I be able to switch the position of the key and value field around?
type StructA struct {
Key string `json:"key"`
Value string `json:"value"`
}
type StructB struct {
Value string `json:"value"`
Key string `json:"key"`
}
func main() {
test := []interface{}{
StructA{Key: "test", Value: "wep"},
StructB{Value: "wep", Key: "test"},
}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
https://play.golang.org/p/72TWDU1BMaL
If you're using json.Marshal provided by official package it's not passible to do that. In stead, you can implement your own MarhalJSON method so that you can decide the position of your struct fields.
To the question "can I make a single struct type serialize its fields in a different order or different occasions ?" :
there isn't a simple way to do this.
Regarding struct fields : the code you write shows the assigning operations in a given order, but once your code is compiled, there is no trace of that order anymore.
If you really need this (side note : I would be curious to know about your use case ?), you could create a custom type, with its own .MarshalJSON() method, which would hold a value indicating "fields should be serialized in this order".
If what you need is to send an array of heterogeneous objects, use separate struct types, and an []interface{} array :
type Obj1 struct{
Key string `json:"name"`
Value string `json:"value"`
}
type Obj2 struct{
Value string `json:"value"`
Key string `json:"name"`
}
func main() {
test := []interface{}{ Obj1{Key: "test",Value: "wep"}, Obj2{Value: "wep",Key: "test"}}
bytes, _ := json.Marshal(test)
fmt.Print(string(bytes))
}
https://play.golang.org/p/TOk28gL0eSK

Append array in Nested struct to another array in another nested struct

I have a nested struct as below
type TaskList struct {
Entries []struct {
Values struct {
TaskID string `json:"Task ID"`
Summary string `json:"Summary"`
Notes interface{} `json:"Notes"`
} `json:"values"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
} `json:"entries"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"_links"`
}
And I want to access 1 Entries struct and append that to another TaskList struct. I'm not really sure how I would be able to do that.
I want to do something along the lines of :
firstList.Entries = append(firstList.Entries,secondList.Entries)
But I get incompatible types, any help on this would be great.
try this way:
firstList.Entries = append(firstList.Entries, secondList.Entries...)

How to insert data in to struct

type Orders struct {
data []struct {
href string `json:"href"`
order_id string `json:"order_id"`
} `json:"data"`
}
How do I insert data in to data array struct in orders struct?
orders.data = append(orders.data, orders.data{ href: r.Host+r.URL.Path+"/"+orderid, order_id: orderid})
it errors. What's wrong?
First see append built-in function.
orders.data is not a type. data is a field with an anonymous struct type of the struct named orders. So you should either name that anonymous struct to something like:
type HrefAndOrderID struct {
href string `json:"href"`
order_id string `json:"order_id"`
}
And use
HrefAndOrderID{"dummy_href", "dummy_order_id"}
when appending.
Otherwise you can again use the same signature of that anonymous struct to append:
orders.data = append(orders.data, struct{href string `json:"href"`; order_id string `json:"order_id"`}{ href: r.Host+r.URL.Path+"/"+orderid, order_id: orderid})

How to query from datastore if the value is in the array struct in go

I have two structs
type Parent struct {
Text string
Childs []ChildValue
}
type ChildValue struct {
Name string
Value int32 `json:"value" datastore:"value" validate:"required,min=1,max=5"`
}
The value in childValue can be from 1 to 5.
I want to be able to filter if value in array is more than 2
query = query.Filter("childs.value =>", "2")
It there a simple way to do that?

Struct with interface type fields

Why is it I can't have this in golang?
type EventDefinition struct {
Name string
EventProperties interface{}
}
Where EventProperties can be one of may types of structs, each struct with different fields. The idea is to have an EventDefinition with EventProperties
type Party struct {
Location string
Hour string
}
or
type Wedding struct {
Bride string
Groom string
Hour string
}
or
type Graduation struct {
Location string
Graduate string
}
Found my problem. The problem was not related to this issue, the problem was
Location : event.Party.Location.(string),
At some point in my implementation I was doing this when Location was a nil interface{}, hence the blowup.
As a response to this, it is possible to do what I mentioned.

Resources