desi Joe
desi Joe

Reputation: 373

how to parse response from AWS Go API

I am using the following sample program:

func getEnv(appName string, env string) {
    svc := elasticbeanstalk.New(session.New(), &aws.Config{Region: aws.String("us-east-1")})

    params := &elasticbeanstalk.DescribeConfigurationSettingsInput{
        ApplicationName: aws.String(appName), // Required
        EnvironmentName: aws.String(env),
    }
    resp, err := svc.DescribeConfigurationSettings(params)

    if err != nil {
        fmt.Println(err.Error())
        return
    }
    v := resp.ConfigurationSettings
    fmt.Printf("%s", v)
}

It's printing out the following response; this looks like a valid json except for the missing quote makes. ex: ApplicationName and not "ApplicationName".

How do I parse this? or get a valid json from AWS?

ConfigurationSettings: [{
          ApplicationName: "myApp",
          DateCreated: 2016-01-12 00:10:10 +0000 UTC,
          DateUpdated: 2016-01-12 00:10:10 +0000 UTC,
          DeploymentStatus: "deployed",
          Description: "Environment created from the EB CLI using \"eb create\"",
          EnvironmentName: "stag-myApp-app-s1",
          OptionSettings: [
            ...

Upvotes: 0

Views: 1380

Answers (1)

Herman Schaaf
Herman Schaaf

Reputation: 48485

resp.ConfigurationSettings is not in JSON format any more, the aws-sdk-go package handled that for you. When you do,

v := resp.ConfigurationSettings

v contains an instance []*ConfigurationSettingsDescription that was parsed from the JSON response, and you don't have to parse it yourself. What you are seeing when you print it out is the Go struct representation. You can just go ahead and use it:

if len(v) > 0 {
    log.Println(v[0].ApplicationName)
}

This should print out myApp

Upvotes: 2

Related Questions