Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Reading CSV Files from Issue Attachment

Joshua Hindman June 17, 2025

Been racking my brains for a couple days going through the forums looking for a way to do what I thought would be a simple task. That's to read a CSV file attached to a ticket then parse it down into something useable. 

Finally came up with the following solution that I'm sure could be finessed but works. Thought I would share here incase anyone else ran into a similar need.

 

import com.mindprod.csv.CSVReader
import com.atlassian.jira.issue.attachment.Attachment


Map csvFromAttachment (Attachment attachment, Boolean hasHeaders = false, char delim = ',', char quote = '\"', Boolean allowMultiLine = true, Boolean trim = true) {

    Map res = [headers: null, lines: []]

    Integer cnt = 0

    attachment.withInputStream { stream -> {

        CSVReader reader = new CSVReader(stream.newReader(),delim, quote, allowMultiLine, trim)

        try {

            while (reader) {

                if (cnt == 0 && hasHeaders) res.headers = reader.getAllFieldsInLine()

                else (res.lines as List).add(reader.getAllFieldsInLine())

                cnt++

            }

        } catch (EOFException e) {

            reader.close()

        } catch (e) {

            // Handle later

        }

    }}

    return res

}

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
June 17, 2025

Hi @Joshua Hindman

Could you elaborate a little bit more? Do you intend to read through the CSV file and pass the value to the fields?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,
Ram

Joshua Hindman June 17, 2025

For this particular instance I was looking to parse an attachment object into a map that could be utilized by other functions. The above code is functional and works as intended. Apologies if this was not the best place to post the solution but I wanted the community to have access to the solution in case a similar need arose. There was no straight forwards solution on forums and many of the links in the older posts are broken.

Simply sharing a solution that I'm sure individuals can use and / or improve upon. If this needs moved or removed that's fine.

Like Evgenii likes this

Suggest an answer

Log in or Sign up to answer