Validate a text box content is a Valid JSON.

Oded Priva August 9, 2014

Hi,

1. I need to validate that a text box is content is a valid JSON.

2. I also need to validate its keys and attributes.

I guess I can do it with script runner but I don't know where to start.

Any help would do .

2 answers

1 accepted

0 votes
Answer accepted
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2014

You can just try/catch with groovy.json.JsonParser#parse(java.lang.String)

Oded Priva August 18, 2014

unable to resolve class groovy.json.JsonParser ...

how do I add it to my jira?

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 18, 2014

try JsonSlurper...

Oded Priva August 19, 2014
import groovy.json.JsonSlurper;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;

IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
MutableIssue Issue = issueManager.getIssueObject("DEMO-1234");

CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField jsonObject = customFieldManager.getCustomFieldObjectByName("DEMO");
def jsonResponse = Issue.getCustomFieldValue(jsonObject)

def slurper = new JsonSlurper()

try {
	def result = slurper.parseText(jsonObject)
        return true
} catch (any) {
	return false;
}

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 9, 2014

You need a "validator" to do this, you can add one to the workflow transitions.

Start at https://jamieechlin.atlassian.net/wiki/display/GRV/Validators

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events