Golang Facebook response to struct - go

Hi there I'm new in GO and I'm trying to convert a json from the facebook api to struct.
The problem is that the keys of the object are dinamic:
{
"100555213756790": {
"id": "100555213756790",
"about": "Noodle Bar & Restaurant",
"metadata": {
"fields": [
{
"name": "id",
"description": "asdasdasdasd",
"type": "numeric string"
},
//...
,
"101285033290986": {
"id": "101285033290986",
"about": "Smart City Expo World Congress",
"metadata": {
"fields": [
{
"name": "id",
"description": "fgddgdfgdg",
"type": "numeric string"
},
what I have achieved so far is extract the objects by id and turn them into a map:
for _, id := range ids {
fbPages, ok := results[string(id)].(map[string]interface{})
if ok {
for k, v := range fbPages {
fmt.Println(k)
fmt.Println(v)
}
}
}
//json to Page struct?
type Page struct {
ID string `json:"id"`
About string `json:"about"`
}
type Metadata struct {
Fields []Field `json:"fields"`
Type string `json:"type"`
Connections map[string]string `json:"connections"`
}
type Field struct {
Name string `json:"name"`
Description string `json:"description"`
Type *string `json:"type,omitempty"`
}
My question is:
how can I convert that map to struct? or is there any easy way to do what I'm trying to do?
Thank you

Converting map to struct:
import "github.com/mitchellh/mapstructure"
mapstructure.Decode(myMap, &myStruct)
example
But I would do this:
type Page struct {
ID string `json:"id"`
About string `json:"about"`
//other fields and nested structs like your metadata struct
}
type fbPages map[string]Page

Related

How to handle nil struct variable in struct type

I need to marshal/unmarshal json to struct in golang. Assume the struct is
type A struct {
Id string `json:"id"`
Version string `json:"version"`
Actors []actor `json:"actors`
Payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
}
type payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
type actor struct {
Id string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
}
The actors or payload maybe empty. The json maybe
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [
{
"id": "1234567",
"type": "XXX",
"role": "111"
},
{
"id": "7654321",
"type": "YYY",
"role": "222"
}
],
"payload": ""
}
or
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [],
"payload": {
"name": "XXXX",
"number": "1234567"
}
}
If i follow the struct A design and try to marshal json with payload empty, i have to init as below
a := A{
Id: "78a07cea-be2b-499c-b82b-e4f510260484",
Version: "1.0.0",
Actors: []actor{
actor{
Id: "1234567",
Type: "XXX",
Role: "111",
},
actor{
Id: "7654321",
Type: "YYY",
Role: "222",
},
},
Payload: payload{},
}
Which will result in below json with one empty payload struct
{
"id": "78a07cea-be2b-499c-b82b-e4f510260484",
"version": "1.0.0",
"actors": [
{
"id": "1234567",
"type": "XXX",
"role": "111"
},
{
"id": "7654321",
"type": "YYY",
"role": "222"
}
],
"payload": {
"name":"",
"number":""
}
}
Is there any way i can generate
"payload": ""
instead of blank payload struct? Or is there any other struct design for this kind of json format? BTW i cannot pass nil to Payload struct.
The json.Marshaler interface can be implemented to customize JSON encoding, and the json.Unmarshaler interface for decoding (left as an exercise for the reader):
package main
import (
"encoding/json"
"fmt"
)
type A struct {
Payload payload
}
type payload struct {
Name string `json:"name"`
Number string `json:"number"`
}
func (p payload) MarshalJSON() ([]byte, error) {
if p.Name == "" && p.Number == "" {
return []byte(`""`), nil
}
type _payload payload // prevent recursion
return json.Marshal(_payload(p))
}
func main() {
var a A
b, _ := json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
a.Payload.Name = "foo"
b, _ = json.MarshalIndent(a, "", " ")
fmt.Println(string(b))
}
// Output:
// {
// "Payload": ""
// }
// {
// "Payload": {
// "name": "foo",
// "number": ""
// }
// }
Try it on the playground: https://play.golang.org/p/9jhSWnKTnTf
The ad-hoc _payload type is required to prevent recursion. If one would write return json.Marshal(p), the json package would call MarshalJSON again, because p is of type payload, and payload implements json.Marshaler.
The _payload type has the same underlying type as payload but does not implement json.Marshaler (see Type definitions for details), so it is encoded using the standard rules of the json package; it produces exactly the same output that encoding a value of type payload would produce if payload didn't implement json.Marshaler.
Check if using omitempty with json struct tag helps. I think it will result in "payload": {} instead of "payload": ""

Unmarshal an array nested inside an JSON object

I am having some problems unmarshalling this JSON data to a Go slice of items that holds item structs:
response := {
"data": [
{
"name": "a",
"products": [
{
"name": "c"
}
]
},
{
"name": "b",
"products": [
{
"name": "d"
}
]
},
{
"name": "c",
"products": [
{
"name": "e"
}
]
}
]
}
These are my structs:
type Item struct {
Name string
Products
}
type Products struct {
Name string
}
The slice should basically be the value the "data" attribute (which is an array) transformed to a Go Items slice. I tried the following, but I kept getting an empty array
var items []Item{}
json.Unmarshal(response, &items)
fmt.Println(items)
You're trying to unmarshal that JSON into a slice, but the root of that JSON is an object (you can tell because the JSON begins with a {, indicating the start of an object). You need to account for that outer layer:
type Payload struct {
Data []Item
}
var payload Payload
json.Unmarshal(response, &payload)
fmt.Println(payload.Data)
I would recommend using this cool tool to help you design your struct
https://mholt.github.io/json-to-go/
Also as Adrian said you're trying to unmarshaling json with an array to an object.
type Product struct {
Name string `json:"name"`
}
type Response struct {
Data []struct {
Name string `json:"name"`
Products []Product `json:"products"`
} `json:"data"`
}

parsing nested JSON with go

I am trying to parse a nested json on GO ,
the json looks like this:
{
"id" : 12345656,
"date" : "2018-05-02-18-16-17",
"lists" : [
{
"empoyee_id": "12343",
"name": "User1"
},
{
"contractor_id" : "12343",
"name": "User1"
},
{
"contractor_id" : "12343",
"name": "User1"
}
]
}
My struct
type Result struct {
id int64 `json:"id"`
Date string `json:"date"`
Lists []string `json:"lists"`
}
I am trying to access it using the following:
var result Result
json.Unmarshal(contents, &result)
How can I change the above to access to the employee_id or the contractor_id fields ?
You need to use another type to store the nested data rather than a slice of strings like so:
package main
import (
"fmt"
"encoding/json"
)
var contents string = `{
"id" : 12345656,
"date" : "2018-05-02-18-16-17",
"lists" : [
{
"empoyee_id": "12343",
"name": "User1"
},
{
"contractor_id" : "12343",
"name": "User1"
},
{
"contractor_id" : "12343",
"name": "User1"
}
]
}`
type Result struct {
ID int64 `json:"id"`
Date string `json:"date"`
Lists []Contractor `json:"lists"`
}
type Contractor struct {
ContractorID string `json:"contractor_id"`
EmployeeID string `json:"employee_id"`
Name string `json:"name"`
}
func main() {
var result Result
err := json.Unmarshal([]byte(contents), &result)
if err != nil {
panic(err)
}
fmt.Println(result)
}
Executable:
https://play.golang.org/p/7dYArgz1V8y
If you just want a single ID field on the nested object then you will need to do a custom unmarshal function on the result to work out which ID is present.

golang mgo modelling issue

I have this model data which I use to save data to the database
type Nos struct {
UnitCode string `json:"unitCode" bson:"unitCode"`
Version string `json:"version" bson:"version"`
Reviews struct {
ReviewCommentsHistory []reviewCommentsHistory `json:"reviewCommentsHistory" bson:"reviewCommentsHistory"`
}
ID bson.ObjectId `bson:"_id"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
}
type reviewCommentsHistory struct {
ReviewHistoryDate time.Time `json:"reviewHistoryDate" bson:"reviewHistoryDate,omitempty"`
}
My mongodb data is as follows
{
"_id" : ObjectId("5a992d5975885e236c8dc723"),
"unitCode" : "G&J/N3601",
"version" : "3",
"Reviews" : {
"reviewCommentsHistory" : [
{
"reviewHistoryDate" : ISODate("2018-04-28T18:30:00.000Z")
}
]
}
}
Using golang package mgo I have written the following piece of code to get the document
func (nosDal NosDal) FindNos(unitCode string, version string) ([]model.Nos, error) {
var result []model.Nos
var err error
col := repository.DB.C("nos")
err = col.Find(bson.M{"unitCode": strings.ToUpper(unitCode), "version": version}).All(&result)
fmt.Println(result[0])
return result, err
}
My response returns the value of null for Reviews.reviewCommentsHistory. Is there an issue with my model? Any pointers would be useful on how to check if the response is mapping to my model
This is my output
{
"unitCode": "G&J/N3601",
"version": "3",
"Reviews": {
"reviewCommentsHistory": null
},
"ID": "5a992d5975885e236c8dc723",
"CreatedAt": "2018-03-02T16:24:17.19+05:30",
"UpdatedAt": "2018-03-05T18:04:28.478+05:30"
}
The problem is that for the Nos.Reviews field you did not specify any bson tag, which means the default mapping will be applied, which means the field name will be used with lowercase letter: "reviews". But your MongoDB contains the document with a capital letter: "Reviews", so the mapping will fail (unmarshaling will not match the MongoDB "Reviews" to the Nos.Reviews field).
Specify the missing tag:
Reviews struct {
ReviewCommentsHistory []reviewCommentsHistory `json:"reviewCommentsHistory" bson:"reviewCommentsHistory"`
} `json:"Reviews" bson:"Reviews"`
And it will work.

Decode 2nd level response body to struct

I have 2 structs:
type List struct {
ListID string `json:"listid"`
Name string `json:"name"`
Users []User `json:"users"`
}
type User struct {
Email string `json:"email"`
Name string `json:"name"`
}
I am calling an endpoint and successfully getting a response which has the structure below:
{
"Results":[
{"Email": "user1#domain.com", "Name": "test1" "State": "Active",…},
{"Email": "user2#domain.com", "Name": "test2" "State": "Active",…},
{"Email": "user3#domain.com", "Name": "test3", "State": "Active",…}
],
"SomeOtherStuff": "email"
}
I am trying to decode the JSON response to my struct like this:
err = json.NewDecoder(response.Body).Decode(&list.Users)
But there is no "Results" attribute in my struct to map to. How can I map only the Results key of the response to my array of User structs ?
To get your data there are at least two options:
Decode into map[string]interface{}
m := create(map[string]interface{})
err = json.NewDecoder(response.Body).Decode(&m)
Then use the m["results"] key to get at your users.
Or you could Decode into a container struct then assign list.Users = container.Results.
type Container struct {
Results []User `json:"Results"`
SomeOtherStuff string `json:"SomeOtherStuff"`
}
To get an idea of structs for arbitrary json look at json2go.

Resources