0

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!

  • 1
    Check the err of json.Unmarshal – Eklavya May 26 '20 at 06:22
  • 1
    I think this is a problem with your JSON decoding. Please try creating a simple test program with the string your are trying to decode plus the unmarshalling code. Isolating the problem would allow people (even you) to more easily understand it. (Eg I know nothing about AWS but a lot about JSON decoding in Go.) – AJR May 26 '20 at 06:24
  • I did look through the logs and seem to have found a potential problem. How could I decode such a string into a JSON format? –  May 26 '20 at 06:31
  • 1
    `awsutil.Prettify` gives your string represtation on data , use `jsonData, err := json.Marshal(connectResultFlow)` to get the json data and then `json.Unmarshal(jsonData, &contactFlowDetails)` or set maually data in struct. – Eklavya May 26 '20 at 06:39
  • Thanks a lot, that solved the problem :) –  May 26 '20 at 06:49

1 Answers1

2

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)
Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92