I want to convert a markdown table to a json string using Scriptrunner for Jira Server (Java/Groovy).
The table is stored in the description field of the issue and looks something like this when displaying the issue through the rest api /rest/api/2/issue
"description": "|| ||Due Date||Done on||OK/NOK||Remarks||\r\n|*Task 1*| | | | |\r\n|*Task 2*| | | | |\r\n|*Task 3*| | | | |\r\n|*Task 4*| 15/03/23| | | See document X|"
For clarity, this is what the table looks like in interpreted MD:
Due Date | Done on | OK/NOK | Remarks | |
---|---|---|---|---|
Task 1 | ||||
Task 2 | ||||
Task 3 | ||||
Task 4 | 15/03/23 | See document X |
This to be converted in the following Json (with a block for each line)
{
"ACTION": [{
"DONE ON": "",
"DUE DATE": "15/03/23",
"NOTE": "See document X",
"TITLE": "Task 4"
}
...
]
}
I have already figured out the regular expression to be used: https://regex101.com/r/Ph5f0a/1
Note that the table is always going to be the same, same number of lines and columns with this specific formatting.
Thing is when I display the content of the issue's description, the \r\n are interpreted as newlines.
Is there a way to display the content as a single line "raw" string so that my regex could work?
Here is the code snippet I used for now.
import com.atlassian.jira.component.ComponentAccessor
import java.io.IOException
import com.atlassian.jira.issue.Issue
import groovy.json.*
import org.apache.log4j.Level
import java.util.regex.Matcher;
import java.util.regex.Pattern;
log.setLevel(Level.INFO);
def issue = issueManager.getIssueObject('PTIJ-68')
def description_raw = issue.getDescription()
log.info("issue.getDescription() = ${issue.getDescription().toString()}")
Thanks.
Some simplistic regex with CSV handling:
import groovy.json.JsonOutput
String description = "|| ||Due Date||Done on||OK/NOK||Remarks||\r\n|*Task 1*| | | | |\r\n|*Task 2*| | | | |\r\n|*Task 3*| | | | |\r\n|*Task 4*| 15/03/23| | | See document X|"
List data = []
description.splitEachLine( /\*?\|+\*?/ ){ _, title, dueDate, doneOn, ok_nok, remarks ->
data << [ 'TTILE':title, "DUE DATE":dueDate.trim(), "DONE ON":doneOn.trim(), "NOTE":remarks.trim() ]
}
data.remove 0 // skip the head
JsonOutput.prettyPrint JsonOutput.toJson( ACTION:data )
prints:
{
"ACTION": [
{
"TTILE": "Task 1",
"DUE DATE": "",
"DONE ON": "",
"NOTE": ""
},
{
"TTILE": "Task 2",
"DUE DATE": "",
"DONE ON": "",
"NOTE": ""
},
{
"TTILE": "Task 3",
"DUE DATE": "",
"DONE ON": "",
"NOTE": ""
},
{
"TTILE": "Task 4",
"DUE DATE": "15/03/23",
"DONE ON": "",
"NOTE": "See document X"
}
]
}