Validate a text box content is a Valid JSON.

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 vote

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

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

how do I add it to my jira?

try JsonSlurper...

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 vote

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Marketplace Apps

Three tips for boosting your board's efficiency with Story Maps

Trello is one of the most effective tools for driving your sprints. It's customizable for every Agile team and product owners and Scrum masters (SM) love it. However, Agile teams often struggle with:...

843 views 2 9
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you