Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang and JSON with array of struct

Tags:

I would like to create a JSON of a GatewayInfo where the type are defined like this:

type SpanInfo struct {     imsi string     network string     network_status string     signal_quality int     slot int     state string }  type GatewayInfo []SpanInfo 

The gateway information is created with:

var gatewayInfo = make(GatewayInfo, nb_spans) 

To create the JSON, I use the json.Marshal function:

gatewayInfo := getGatewayInfo(spans) log.Printf("Polling content: %s\n", gatewayInfo)  jsonInfo, _ := json.Marshal(gatewayInfo) log.Printf("jsonInfo: %s\n", jsonInfo) 

Unfortunately the result is not what I was expecting:

2015/02/09 13:48:26 Polling content: [{652020105829193 20801 Registered (Roaming) %!s(int=17) %!s(int=2) } {652020105829194 20801 Registered (Roaming) %!s(int=16) %!s(int=3) } {652020105829192 20801 Registered (Roaming) %!s(int=19) %!s(int=1) } {652020105829197 20801 Registered (Roaming) %!s(int=19) %!s(int=4) }] 2015/02/09 13:48:26 jsonInfo: [{},{},{},{}] 

As we can see, the GatewayInfo instance has the SpanInfo, but in the JSON I have empty SpanInfo.

like image 400
Julio Avatar asked Feb 09 '15 13:02

Julio


People also ask

How to encode a struct to JSON in Golang?

We can encode the Struct to JSON using the JSON package in Go. JSON pkg has the Marshal and MarshalIndent function that returns the JSON encoding of the given input. MarshalIndent is like Marshal but applies Indent to format the output.

How to unmarshal JSON strings in go?

We mostly deal with complex objects or arrays when working with JSON, but data like 3, 3.1412 and "birds" are also valid JSON strings. We can unmarshal these values to their corresponding data type in Go by using primitive types: We saw earlier that Go uses convention to determine the attribute name for mapping JSON properties.

How do I ignore a field in a JSON array?

If we want to always ignore a field, we can use the json:"-" struct tag to denote that we never want this field included: This isn’t much different from structs. We just need to pass the slice or array to the json.Marshal function, and it will encode data like you expect: We can use maps to encode unstructured data.

What is unmarshal in Golang?

The Unmarshal function provided by Go’s JSON standard library lets us parse raw JSON data in the form of []byte variables. We can convert JSON strings into bytes and unmarshal the data into a variables address:


1 Answers

Your struct fields must be exported (field is exported if it begins with a capital letter) or they won't be encoded:

Struct values encode as JSON objects. Each exported struct field becomes a member of the object

To get the JSON representation as probably expected change the code to this:

type SpanInfo struct {     IMSI string `json:"imsi"`     Network string `json:"network"`     NetworkStatus string `json:"network_status"`     SignalQuality int `json:"signal_quality"`     Slot int `json:slot"`     State string `json:"state"` }  type GatewayInfo []SpanInfo 
like image 107
Arjan Avatar answered Sep 17 '22 09:09

Arjan