Search code examples
jsonamazon-web-servicesapigoamazon-connect

Parsing data from AWS api using Golang


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!


Solution

  • 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)