How to unmarshal this nested JSON into go objects? - json

I have a JSON object similar to this one:
{
"prices": {
"7fb832f4-8041-4fe7-95e4-6453aeeafc93": {
"diesel": 1.234,
"e10": 1.234,
"e5": 1.234,
"status": "open"
},
"92f703e8-0b3c-46da-9948-25cb1a6a1514": {
"diesel": 1.234,
"e10": 1.234,
"e5": 1.234,
"status": "open"
}
}
I am not sure how to unmarshal this into an GO object without losing the unique ID field of each sub-item which is important information for me.

You can use a map with string keys to preserve the unique IDs of each sub-price:
type Object struct {
Prices map[string]*Price `json:"prices"`
}
type Price struct {
Diesel float32 `json:"diesel"`
E10 float32 `json:"e10"`
E5 float32 `json:"e5"`
Status string `json:"status"`
}
Then, for example, you could loop over the unmarshaled object:
for id, price := range o.Prices {
fmt.Printf("%s %v\n", id, price)
}
https://play.golang.org/p/aPhvGdtFC_

Use a map:
type Station struct {
Diesel float64
E10 float64
E15 float64
Status string
}
type Data struct {
Prices map[string]*Station
}
playground example

Related

How to convert string to JSON in Go?

Let’s say I have this string:
`
{
"testCode": 0,
"replyTest": "OK",
"data": {
"001": {
"fields": {
"name": "arben",
"fav_color": "blue",
"address": "PH",
}
},
"002": {
"fields": {
"name": "john",
"fav_color": "black",
"address": "PH",
}
},
}
}
`
How to convert this string to JSON where data is in form of list in order for me to loop this list in a process?
Whenever you have json property names that aren't known upfront, or they don't lend themselves very well to be represented as fields of a struct then you're left more or less with one option, a map. So first you need to unmarshal the "data" property into a map.
However maps in Go are implemented as unordered groups of elements indexed by a set of unique keys. So basically there's no way to ensure the order of a map in Go, and therefore you'll have to transfer the data from the map into something that can be ordered, like a slice for example.
After you've got your data in the slice you can use the standard sort package to sort the slice in the order you want.
You can start by declaring the types you need:
type DataItem struct {
Key string `json:"-"`
Fields DataFields `json:"fields"`
}
type DataFields struct {
Name string `json:"name"`
FavColor string `json:"fav_color"`
Address string `json:"address"`
}
Then unmarshal the json
var obj struct {
TestCode int `json:"testCode"`
ReplyTest string `json:"replyTest"`
Data map[string]*DataItem `json:"data"`
}
if err := json.Unmarshal(data, &obj); err != nil {
panic(err)
}
Transfer the contents of the map into a slice
items := []*DataItem{}
for key, item := range obj.Data {
item.Key = key // keep track of the key because it will be used to order the contents of the slice
items = append(items, item)
}
Finally sort the slice
sort.Slice(items, func(i, j int) bool {
return items[i].Key < items[j].Key
})
https://play.golang.com/p/D2u46veOQwD

cannot unmarshal array into Go struct when decoding array

I'm trying to properly decode JSON String to an object.
I defined the following structs:
type AjaxModelsList struct {
Id float64 `json:"Id"`
Name string `json:"Name"`
CarId float64 `json:"CarId"`
EngName string `json:"EngName"`
}
type AjaxModelsData struct {
ModelList []AjaxModelsList `json:"ModelList"`
}
type AjaxModels struct {
Status bool `json:"status"`
Data map[string]AjaxModelsData `json:"data"`
}
the defined object is
{
"status": true,
"data": {
"ModelList": [{
"Id": 1,
"Name": "foo",
"CarId": 1,
"EngName": "bar"
}]
}
}
and I unmarshall using the following code:
c :=AjaxModels{}
if err := json.Unmarshal(body_byte,&c); err != nil {
log.Fatalf("an error occured: %v",err)
}
and I get the following output:
an error occured: json: cannot unmarshal array into Go struct field AjaxModels.data of type main.AjaxModelsData
since I used []AjaxModelsList it's a slice so I shouldn't be getting this error. I probably missed something, but what ?
In the json the data structure is object[key]array, while in Go Data is map[key]struct.slice. Change Data to be map[key]slice instead.
E.g.
type AjaxModels struct {
Status bool `json:"status"`
Data map[string][]AjaxModelsList `json:"data"`
}
https://play.golang.org/p/Sh_vKVL-D--

How to show empty object instead of empty struct or nil in Go json marshal

I need to show json's empty object {} when do json.Marshal() for a struct pointer. I can only output either null value or empty struct value.
If the person key is filled with &Person{} or new(Person), it will show empty struct like below:
{
"data": {
"person": {
"name": "",
"age": 0
},
"created_date": "2009-11-10T23:00:00Z"
}
}
And if we don't initialize it at all, it will show null.
{
"data": {
"person": null,
"created_date": "2009-11-10T23:00:00Z"
}
}
I want to show "person": {}. Is it possible?
Go Playground for the complete code: https://play.golang.org/p/tT15G2ESPVc
Option A, use the omitempty tag option on all of the Person's fields and make sure the response's field is allocated before marshaling.
type Person struct {
Name string `json:"name,omitempty"`
Age int `json:"age,omitempty"`
}
// ...
resp.Person = new(Person)
https://play.golang.org/p/o3jWdru_8bC
Option B, use a non-pointer wrapper type that embeds the Person pointer type.
type PersonJSON struct {
*Person
}
type Response struct {
Person PersonJSON `json:"person"`
CreatedDate time.Time `json:"created_date"`
}
https://play.golang.org/p/EKQc7uf1_Vk
Option C, have the Reponse type implement the json.Marshaler interface.
func (r *Response) MarshalJSON() ([]byte, error) {
type tmp Response
resp := (*tmp)(r)
var data struct {
Wrapper struct {
*Person
} `json:"person"`
*tmp
}
data.Wrapper.Person = resp.Person
data.tmp = resp
return json.Marshal(data)
}
https://play.golang.org/p/1qkSCWZ225j
There may be other options...
In Go, an empty struct by definition assigns zero values to field elements. Eg: for int 0, "" for string, etc.
For your case, simply comparing to null would work out. Or, you could define an emptyPerson as:
var BAD_AGE = -1
emptyPerson := &Person{"", BAD_AGE} // BAD_AGE indicates no person
if person[age] == BAD_AGE {
// handle case for emptyPerson}

Unmarshal JSON in go with different types in a list

I have trouble unmarschaling a JSON contruct:
{
"id": 10,
"result": [
{
"bundled": true,
"type": "RM-J1100"
},
[
{
"name": "PowerOff",
"value": "AAAAAQAAAAEAAAAvAw=="
},
{
"name": "Input",
"value": "AAAAAQAAAAEAAAAlAw=="
}
]
]
}
I actually need the second slice item from the result.
My current attempt is
type Codes struct {
Id int32 `json:"id"`
Result []interface{} `json:"result"`
}
type ResultList struct {
Info InfoMap
Codes []Code
}
type InfoMap struct {
Bundled bool `json:"bundled"`
Type string `json:"type"`
}
type Code struct {
Name string `json:"name"`
Value string `json:"value"`
}
the output is like:
{10 {{false } []}}
but I also tried to use this:
type Codes struct {
Id int32 `json:"id"`
Result []interface{} `json:"result"`
}
the output is okay:
{10 [map[type:RM-J1100 bundled:true] [map[name:PowerOff value:AAAAAQAAAAEAAAAvAw==] map[name:Input value:AAAAAQAAAAEAAAAlAw==]]]}
I can also reference the Result[1] index:
[map[name:PowerOff value:AAAAAQAAAAEAAAAvAw==] map[name:Input value:AAAAAQAAAAEAAAAlAw==]]
But I fail to convert the interface type to any other Type which would match. Can anybody tell me how to do interface conversion. And what approach would be the "best".
One option would be to unmarshal the top level thing into a slice of json.RawMessage initially.
Then loop through the members, and look at the first character of each one. If it is an object, unmarshal that into your InfoMap header struct, and if it is an array, unmarshal it into a slice of the Code struct.
Or if it is predictable enough, just unmarshal the first member to the one struct and the second to a slice.
I made a playground example of this approach.
type Response struct {
ID int `json:"id"`
RawResult []json.RawMessage `json:"result"`
Header *Header `json:"-"`
Values []*Value `json:"-"`
}
type Header struct {
Bundled bool `json:"bundled"`
Type string `json:"type"`
}
type Value struct {
Name string `json:"name"`
Value string `json:"value"`
}
func main() {
//error checks ommitted
resp := &Response{}
json.Unmarshal(rawJ, resp)
resp.Header = &Header{}
json.Unmarshal(resp.RawResult[0], resp.Header)
resp.Values = []*Value{}
json.Unmarshal(resp.RawResult[1], &resp.Values)
}
(I will not point out how horrific it is this JSON struct, but as always: sXXt happens)
You can convert your struct like this, by using a cycle of JSON Marshal / Unmarshal. Code follows:
package main
import (
"encoding/json"
"log"
)
const (
inputJSON = `{
"id": 10,
"result": [
{
"bundled": true,
"type": "RM-J1100"
},
[
{
"name": "PowerOff",
"value": "AAAAAQAAAAEAAAAvAw=="
},
{
"name": "Input",
"value": "AAAAAQAAAAEAAAAlAw=="
}
]
]
}`
)
type Codes struct {
Id int32 `json:"id"`
Result [2]interface{} `json:"result"`
}
type Result struct {
Info InfoMap
Codes []Code
}
type InfoMap struct {
Bundled bool `json:"bundled"`
Type string `json:"type"`
}
type Code struct {
Name string `json:"name"`
Value string `json:"value"`
}
func main() {
newCodes := &Codes{}
err := json.Unmarshal([]byte(inputJSON), newCodes)
if err != nil {
log.Fatal(err)
}
// Prints the whole object
log.Println(newCodes)
// Prints the Result array (!)
log.Println(newCodes.Result)
if len(newCodes.Result) != 2 {
log.Fatal("Invalid Result struct")
}
// Marshal and Unmarshal data to obtain the code list
byteCodeList, _ := json.Marshal(newCodes.Result[1])
codeList := make([]Code, 0)
err = json.Unmarshal(byteCodeList, &codeList)
if err != nil {
log.Fatal("Invalid Code list")
}
// Prints the codeList
log.Println(codeList)
}
Test it on playground.

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",
"Season":"Winter"}
]}
}
I am trying to create Go lang model to unmarshal the contents. Following is the code I have tried:
type Response struct {
Count int
Fruits []Fruit
}
type Fruit struct {
Name string
Season string
}
But when I marshal the Response object in my test code I don't see similar json. (https://play.golang.org/p/EGKqfbwFvW)
Marshalled data always appears as :
{
"Count":100,
"Fruits":[
{"Name":"Mango","Season":"Summer"},
{"Name":"Fig","Season":"Winter"}
]
}
Notice the Fruits appearing as array [] and not {[]} in original json. How can I model structs in golang for this response?
Your model is totally correct and valid, but the JSON object is not. "Fruits" doesn't have name if it should be key value pair or it should be wrapped in [] not {}.
JSON obj should be formatted like this:
{
"Basket" : {
"Count": 1,
"Fruits": [
{
"Name":"Mango",
"Season":"Summer"
},
{
"Name":"Fig",
"Season":"Winter"
}
]
}
}
And actually invalid json shouldn't work https://play.golang.org/p/yoW7t4NfI7
I would make 'Baskets' a struct within 'Response', create a 'BasketsData' struct, and give it all some labels.
type Fruit struct {
Name string `json:"Name"`
Season string `json:"Season"`
}
type BasketData struct {
Count int `json:"Count"`
Fruits []Fruit `json:"Fruits"`
}
type Response struct {
Basket BasketData `json:"Basket"`
}
This way you will get a top level JSON response when you marshall it.
fruitmania := []Fruit{{Name: "Mango", Season: "Summer"},
{Name: "Fig", Season: "Winter"}}
basket := Response{BasketData{Count: 100, Fruits: fruitmania}}
b, _ := json.Marshal(basket)
fmt.Println(string(b))
checkit-checkit out:
https://play.golang.org/p/TuUwBLs_Ql