When trying to get the details of an issue through the REST API the response for the Sprint looks something like this:
It looks like toString of the Sprint class which isn't very easy to parse/read with an application. Is their a way to simple get the Sprint Name or id returned through the REST API. It's necessary to be able to retrieve the Sprint value in an internal tool for our QA Team (Spira Test).
On the other side, using toString in the REST API output doesn't seem like a good idea to me ...
Thanks,
Maarten
I was having the same issue for a small tool I was developing in which I needed the sprint name. Since I was using Jackson to unmarshal my JIRA response, I ended up writing a custom deserializer:
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import static java.util.stream.Collectors.toList;
public class SprintNamesExtractingDeserializer extends JsonDeserializer<Collection> {
private static final String OPEN_ARRAY_CHARACTER = "\\[";
private static final String CLOSE_ARRAY_CHARACTER = "]";
private static final String COMMA = ",";
private static final String EQUALS_SIGN = "=";
private static final String NAME_PROPERTY = "name";
public SprintNamesExtractingDeserializer() {
}
@Override
public Collection deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
TypeReference<Collection<String>> typedCollection = new TypeReference<Collection<String>>() {
};
Collection<String> sprints = jsonParser.readValueAs(typedCollection);
return sprints.stream()
.map(extractSprintName())
.collect(toList());
}
private Function<String, String> extractSprintName() {
return sprintString -> {
String result = sprintString.split(OPEN_ARRAY_CHARACTER, 2)[1];
Map<String, String> sprintProperties = extractSprintProperties(result);
return sprintProperties.getOrDefault(NAME_PROPERTY, null);
};
}
private Map<String, String> extractSprintProperties(String result) {
int lastArrayClosure = result.lastIndexOf(CLOSE_ARRAY_CHARACTER);
result = result.substring(0, lastArrayClosure);
String[] propertiesArray = result.split(COMMA);
HashMap<String, String> properties = new HashMap<>();
for (String property: propertiesArray) {
String[] keyValueArray = property.split(EQUALS_SIGN);
if (keyValueArray.length == 1) {
properties.put(keyValueArray[0], null);
} else if (keyValueArray.length == 2) {
properties.put(keyValueArray[0], keyValueArray[1]);
}
// Else it's not a valid property
}
return properties;
}
}
Which can be used as:
@JsonProperty("customfield_10400")
@JsonDeserialize(using = SprintNamesExtractingDeserializer.class, as=Collection.class)
public abstract Collection<String> getSprintNames();
I'm not particularly proud of this solution, but it does the trick.
@Stef Klaassen, thank you! I used your method extractSprintProperties to get readable data from sprint string retrieved from the field.
No idea why would API return JSONArray with no way to cleanly cast it to array of sprints. I also need to get the state out of sprint and mark active one.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Stef KlaassenI know its an old topic, but your answer is very similar to my solution, and I have a follow-up question: how do you deal with sprint names with '=' or ',' characters, or even (theoretically) something like 'name=sprint=great' or 'name=,name=test,name=123'? Do you just assume that no one will do that, or do you accept it as a risk?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've never encountered that issue, but it's a valid remark. There's probably lots of ways to improve this piece of ugly code to something more robust. :-)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
See my answer at the end (here: https://community.atlassian.com/t5/Jira-questions/Re-Sprint-field-value-REST-API/qaq-p/1630481/comment-id/469401#M469401)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No Answer has been found :-(
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you please give me the rest call url to fetch the data which you have found havent even have that url to fetch that data for me . thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Haven't found a great way, but this works pretty well. Using that Java class seems like it would match best, but honestly we shouldn't have to do this sort of parsing for a JSON REST API.
You can pipe that field through the `jq` utility this way to turn that rubbish Java serialization into desired JSON:
match("[\\\\w.]+?@\\\\w+?\\\\[(.*?)\\\\]").captures[0].string | split(",") | map(split("=") | {"key": .[0], "value": .[1]}) | from_entries
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any idea of one jar containing the mentioned class com.atlassian.greenhopper.service.sprint.Sprint
I assume the response can be parsed something like the following:
Field field = issue.getFieldByName("Sprint");
JSONArray fieldValue = (JSONArray) field.getValue(); com.atlassian.greenhopper.service.sprint.Sprint sprint = (Sprint) fieldValue.get(0);
I just can't find the particular class/jar.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Found it, but it looks too big to include, I think I'll go with parsing it as a String.
Jar info can be found at https://www.versioneye.com/java/com.atlassian.jira.plugins:jira-greenhopper-plugin/6.2
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For JIRA Cloud the string representation has been removed since 1st Sept. 2020: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-tostring-representation-of-sprints-in-get-issue-response/
For Jira SERVER the default behaviour was not changed.
But as a workaround, use the "&expand=versionedRepresentations" parameter instead, find the newest representation and just use the data with the known json representation (key : value structure).
Beware that the json "fields" parent property is replaced by "versionedRepresentations".
See the API doc for "Get issue": https://docs.atlassian.com/software/jira/docs/api/REST/8.5.5/#api/2/issue-getIssue
Don't do a parsing of the string representation as this might be removed for JIRA Server as well in the future and it will break your clients.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
[ignore]Double-post because of spam filter not letting through my code example.[/ignore]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Up?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's not hard to parse that. The toString returns a JSON representation of that object. You just need to convert that String into a JSON object.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's far from JSON ...
JSON would be something like this:
{ rapidViewId: 1, state: "Closed", name: "NORD - Sprint 42", ... }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.