Parsing data from AWS api using Golang - json

I am using the Connect API to get all the contact flows from a particular instance and want to store them in a DynamoDB.
type contactFlow struct {
Arn string
ContactFlowType string
Id string
Name string
}
func HandleRequest(ctx context.Context) (string, error) {
var contactFlowDetails []contactFlow
mySession := session.Must(session.NewSession())
connectSession := connect.New(mySession)
connectInstance := &connect.ListContactFlowsInput{
InstanceId: aws.String("INSTANCE_ID"),
}
connectResult, connectError := connectSession.ListContactFlows(connectInstance)
connectResultFlow := connectResult.ContactFlowSummaryList
connectFlowSummaryList := awsutil.Prettify(connectResultFlow)
fmt.Println(connectFlowSummaryList)
json.Unmarshal([]byte(connectFlowSummaryList), &contactFlowDetails)
fmt.Println(contactFlowDetails)
The API that I am trying to use is this: https://docs.aws.amazon.com/sdk-for-go/api/service/connect/#ListContactFlowsOutput
I do get the result when I print out connectFlowSummaryList on CloudWatch Logs but it always returns an empty array [] when I print out contactFlowDetails.
Edit 1: I think I found what could be the potential problem while doing this decoding. The result from the logs look something like this:
[
{
Arn: "INSTANCE_ID",
ContactFlowType: "AGENT_WHISPER",
Id: "CONTACT_FLOW_ID",
Name: "Default agent whisper"
}
]
The key values of the result are not present inside double-inverted commas, how could I go about decoding such a result?
Thanks!

What you should do is marshal connectResultFlow.ContactFlowSummaryList to a json string before passing it to awsutil.Prettify (if you need to).
You can also, skip awsutil.Prettify completely, to arrive at this:
connectResultFlow := connectResult.ContactFlowSummaryList
b, err := json.Marshal(connectResultFlow)
if err != nil {
return "", err
}
json.Unmarshal(b, &contactFlowDetails)
fmt.Println(contactFlowDetails)

Related

Display JSON object keys in the same order as in the YAML source

I'm working with the library github.com/ghodss/yaml to create JSON based in .yaml files, I'm experiencing that the JSON is not being displayed in the correct order as I have it.
This is my .yaml file:
name: "Foo"
lastname: "Bar"
age: 25
and this is my GoCode:
for _, file := range yamlfiles {
yamlBytes, err := ioutil.ReadFile(file)
if err != nil {
log.Panicln(err)
}
jsonFormat, err := yaml.YAMLToJSON(yamlBytes)
if err != nil {
log.Panicln(err)
}
fmt.Println(string(jsonFormat))
}
And this is the output:
{"age":25,"lastname":"Bar","name":"Foo"}
Expected output:
{"name":"Foo","lastname":"Bar","age":25}
How could I have this in order as I have my .yaml file?
Your data consist of a single (root-level) mapping and the YAML specification explicitly states:
mapping - an unordered association of unique keys to values
A library compliant to the specification loads that mapping in an unordered way as so does your Go YAML-library.¹
So your expectation is definitely incorrect.
It could be coincidence, but it looks like your JSON is sorted by key, and that is a valid option given unsorted input.
¹ The only library that I know not to comply with this is ruamel.yaml for Python when using round-trip mode. Using Python and ruamel.yaml it is fairly trivial to get the output the way you expected.
If you define a struct with those properties then the JSON marshaler will emit keys in the order that the fields were defined (Go Playground):
type Person struct {
Name string `json:"name"`
LastName string `json:"lastname"`
Age int `json:"age"`
}
p := Person{"Foo", "Bar", 25}
bs, err := json.Marshal(&p)
if err != nil {
panic(err)
}
fmt.Println(string(bs))
// {"name":"Foo","lastname":"Bar","age":25}

Insert a slice result JSON into MongoDB

I'm using the mgo driver for MongoDB, with the Gin framework.
type Users struct {
User_id *string `json:"id user" bson:"id user"`
Images []string `json:"images" bson:"images"`
}
I have this function which tries to convert the slice into JSON.
The slice here is UsersTotal
func GetUsersApi(c *gin.Context) {
UsersTotal, err := GetUsers()
if err != nil {
fmt.Println("error:", err)
}
c.JSON(http.StatusOK, gin.H{
"Count Users": len(UsersTotal),
"Users Found ": UsersTotal,
})
session, err := mgo.Dial(URL)
if err == nil {
fmt.Println("Connection to mongodb established ok!!")
cc := session.DB("UsersDB").C("results")
err22 := cc.Insert(&UsersTotal)
if err22 != nil {
fmt.Println("error insertion ", err22)
}
}
session.Close()
}
Running it I get the following error:
error insertion Wrong type for documents[0]. Expected a object, got a array.
Inserting multiple documents is the same as inserting a single one because the Collection.Insert() method has a variadic parameter:
func (c *Collection) Insert(docs ...interface{}) error
One thing you should note is that it expects interface{} values. Value of any type qualifies "to be" an interface{}. Another thing you should note is that only the slice type []interface{} qualifies to be []interface{}, a user slice []User does not. For details, see Type converting slices of interfaces in go
So simply create a copy of your users slice where the copy has a type of []interface{}, and that you can directly pass to Collection.Insert():
docs := make([]interface{}, len(UsersTotal))
for i, u := range UsersTotal {
docs[i] = u
}
err := cc.Insert(docs...)
// Handle error
Also please do not connect to MongodB in your handler. Do it once, on app startup, store the global connection / session, and clone / copy it when needed. For details see mgo - query performance seems consistently slow (500-650ms); and too many open files in mgo go server.

Go: decoding json with one set of json tags, and encoding to a different set of json tags

I have an application that consumes data from a third-party api. I need to decode the json into a struct, which requires the struct to have json tags of the "incoming" json fields. The outgoing json fields have a different naming convention, so I need different json tags for the encoding.
I will have to do this with many different structs, and each struct might have many fields.
What is the best way to accomplish this without repeating a lot of code?
Example Structs:
// incoming "schema" field names
type AccountIn struct {
OpenDate string `json:"accountStartDate"`
CloseDate string `json:"cancelDate"`
}
// outgoing "schema" field names
type AccountOut struct {
OpenDate string `json:"openDate"`
CloseDate string `json:"closeDate"`
}
Maybe the coming change on Go 1.8 would help you, it will allow to 'cast' types even if its JSON tags definition is different: This https://play.golang.org/p/Xbsoa8SsEk works as expected on 1.8beta, I guess this would simplify your current solution
A bit an uncommon but probably quite well working method would be to use a intermediate format so u can use different readers and writers and therefore different tags. For example https://github.com/mitchellh/mapstructure which allows to convert a nested map structure into struct
types. Pretty similar like json unmarshal, just from a map.
// incoming "schema" field names
type AccountIn struct {
OpenDate string `mapstructure:"accountStartDate" json:"openDate"`
CloseDate string `mapstructure:"cancelDate" json:"closeDate"`
}
// from json to map with no name changes
temporaryMap := map[string]interface{}{}
err := json.Unmarshal(jsonBlob, &temporaryMap)
// from map to structs using mapstructure tags
accountIn := &AccountIn{}
mapstructure.Decode(temporaryMap, accountIn)
Later when writing (or reading) u will use directly the json functions which will then use the json tags.
If it's acceptable to take another round trip through json.Unmarshal and json.Marshal, and you don't have any ambiguous field names within your various types, you could translate all the json keys in one pass by unmarshaling into the generic structures used by the json package:
// map incoming to outgoing json identifiers
var translation = map[string]string{
"accountStartDate": "openDate",
"cancelDate": "closeDate",
}
func translateJS(js []byte) ([]byte, error) {
var m map[string]interface{}
if err := json.Unmarshal(js, &m); err != nil {
return nil, err
}
translateKeys(m)
return json.MarshalIndent(m, "", " ")
}
func translateKeys(m map[string]interface{}) {
for _, v := range m {
if v, ok := v.(map[string]interface{}); ok {
translateKeys(v)
}
}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
for _, k := range keys {
if newKey, ok := translation[k]; ok {
m[newKey] = m[k]
delete(m, k)
}
}
}
https://play.golang.org/p/nXmWlj7qH9
This might be a Naive Approach but is fairly easy to implement:-
func ConvertAccountInToAccountOut(AccountIn incoming) (AccountOut outcoming){
var outcoming AccountOut
outcoming.OpenDate = incoming.OpenDate
outcoming.CloseDate = incoming.CloseDate
return outcoming
}
var IncomingJSONData AccountIn
resp := getJSONDataFromSource() // Some method that gives you the Input JSON
err1 := json.UnMarshall(resp,&IncomingJSONData)
OutGoingJSONData := ConvertAccountInToAccountOut(IncomingJSONData)
if err1 != nil {
fmt.Println("Error in UnMarshalling JSON ",err1)
}
fmt.Println("Outgoing JSON Data: ",OutGoingJSONData)

Golang Null Types and json.Decode()

I have not been able to find a way around this issue currently. If I have a structure i would like to populate with json from a http.Request I have no way to tell for instance what value was actually passed in for some values. For instance if I pass in an empty json object and run json.Decode on a structure that looks like this...
var Test struct {
Number int `json:"number"`
}
I now have a json object that supposedly was passed with a key of number and a value of zero when in fact I would rather have this return nothing at all. Does go provide another method that would actually allow me to see what JSON has been passed in or not.
Sorry for the rambling I have been trying to figure out how to to this for a few days now and it's driving me nuts.
Thanks for any help.
Edit:
I made this to depict exactly what I am talking about http://play.golang.org/p/aPFKSvuxC9
You could use pointers, for example:
func main() {
var jsonBlob = []byte(`[
{"Name": "Platypus"},
{"Name": "Quoll", "Order": 100}
]`)
type Animal struct {
Name string
Order *int
}
var animals []Animal
err := json.Unmarshal(jsonBlob, &animals)
if err != nil {
fmt.Println("error:", err)
}
for _, a := range animals {
if a.Order != nil {
fmt.Printf("got order, %s : %d\n", a.Name, *a.Order)
}
}
}
I don't see how you could do this by giving a struct to the Unmarshal function. With the following structure for instance:
type A struct {
Hello string
Foo int
Baz string
}
var a A
json.Unmarshal(data, &a)
Even by doing another implementation of Unmarshal, there would be only two (simple) possibilities:
If baz is not in the json data, set a.Baz to a default value, compatible with its type: the empty string (or 0 if it's an integer). This is the current implementation.
If baz is not in the json data, return an error. That would be very inconvenient if the absence of baz is a normal behaviour.
Another possibility would be to use pointers, and use the default value nil in the same spirit than the default value I talked about, but there would still be issue if your json file could be filled with null values: you would not be able to distinguish values that were in the json file, but set as null, and values that were not in the json, and unmarshalled with nil as their default value.
However, this solution might suit you: instead of using a struct, why not using a map[string]interface{} ? The Unmarshall function would not have to add a default value to non-present fields, and it would be able to retrieve any type of data from the json file.
var b = []byte(`[{"Name": "Platypus"}, {"Name": "Quoll", "Order": 100}]`)
var m []map[string]interface{}
err := json.Unmarshal(b, &m)
fmt.Println(m)
// [map[Name:Platypus] map[Name:Quoll Order:100]]

Catchall type for Golang API response

I am trying to define a struct that can hold an array of any type like so:
type APIResonse struct {
length int
data []interface{}
}
I want the data property to be capable of holding an array of any type/struct so I can have a single response type, that will eventually be serialized to json. So what I want to be able to write is something like the following:
someStruct := getSomeStructArray()
res := &APIResponse{
length: len(someStruct),
data: someStruct,
}
enc, err := json.Marshal(res)
Is this possible in Go? I keep getting cannot use cs (type SomeType) as type []interface {} in assignment. Or do I have to create a different response type for every variation of data? Or maybe I am going about this wrong entirely / not Go-like. Any help would be much appreciated!
There are a couple of problems with that code.
You need to use interface{}, not []interface{}, also [] is called a slice, an array is a fixed number of elements like [10]string.
And your APIResponse fields aren't exported, so json.Marshal will not print out anything.
func main() {
d := []dummy{{100}, {200}}
res := &APIResponse{
Length: len(d),
Data: d,
}
enc, err := json.Marshal(res)
fmt.Println(string(enc), err)
}
playground