-2

I'm using https://godoc.org/github.com/andygrunwald/go-jira#IssueService.GetCustomFields to get a custom field and I'm attempting to consume some of the data.

func getsomedata(issue_id string) {
    issue, _, _ := jiraClient.Issue.Get(issue_id, nil)
    fields, _, _ := jiraClient.Issue.GetCustomFields(issue_id)
    data := fields["customfield_123456"]
}

Something similar to the following (unformatted) is returned as a single string, how can I convert this back into a struct or map? The end goal is to store "key.value" and "name.value"

[
  map[
    key:key.value
    name:name.value
  ]
  map[
    key:key.value
    name:name.value
  ]
]
Jukie
  • 35
  • 1
  • 1
  • 5

1 Answers1

0

I figured it out, type assertion is the way to solve this.

func getsomedata(issue_id string) {
    issue, _, _ := jiraClient.Issue.Get(issue_id, nil)

    // This returns as a slice of interfaces []interfaces{}
    data := issue.Fields.Unknowns["customfield_12345"]

    // Will use the length of the slice in the for loop below
    s := reflect.ValueOf(data)

    // For each index index in the slice, do...
    for i := 0; i < s.Len(); i++ {
        // Use type assertion to get the value I need for all indexes "i"
        d := data.([]interface{})[i].(map[string]interface{})
        fmt.Printf("%v\n", d["key"].(string))
    }
} 
Jukie
  • 35
  • 1
  • 1
  • 5