Issue
I need to iterate a json file within my jenkins pipeline in order to get some values inside the "fields" value such as "System.AreaPath" for instance , the json file has the following structure:
{
"fields": {
"AcornAgileProcess.BugSource": "Ad Hoc",
"AcornAgileProcess.IssueType": "Content",
"Custom.BuildVariant": "Default",
"Custom.ReproinRetail": "No",
"Custom.SQInvestigationStatus": "Not started",
"PhoenixAgileProcess.GameArea": "Art - technical/FX",
"PhoenixAgileProcess.GameMode": "In-Game (all)",
"PhoenixAgileProcess.ReproCount": 4,
"PhoenixAgileProcess.ReproRate": "5 - Consistently (100%)",
"PhoenixAgileProcess.ReproTime": 5,
"PhoenixAgileProcess.SkuTested": "sometext",
"System.AreaId": 1098,
"System.AreaLevel1": "sometext",
"System.AreaPath": "sometext",
"System.AssignedTo": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NmE1MWI2NjMtOWU2Ny03NWYyLTlkOGEtNjMwMDFhMzVlZDI3",
"displayName": "Triage",
"id": "sometext",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedAs": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.AuthorizedDate": "2021-03-31T23:18:58.513Z",
"System.ChangedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.YmJkMGZjNmItMTBjYi03ODYxLTk0NTAtNGZjNGYwYjkzMmM3",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.ChangedDate": "2021-03-31T23:18:58.513Z",
"System.CommentCount": 0,
"System.CreatedBy": {
"_links": {
"avatar": {
"href": "someurl"
}
},
"descriptor": "aad.NTcyMTBjZmQtY2VlOC03NjFhLTk4YzctYTc2YjA4NDUyZDU5",
"displayName": "sometext",
"id": "someid",
"imageUrl": "someurl",
"uniqueName": "sometext",
"url": "someurl"
},
"System.CreatedDate": "2021-03-31T23:16:45.42Z",
"System.Id": 896092,
"System.IterationId": 1093,
"System.IterationLevel1": "sometext",
"System.IterationPath": "sometext",
"System.NodeName": "sometext",
"System.PersonId": 31919392,
"System.Reason": "New",
"System.Rev": 2,
"System.RevisedDate": "9999-01-01T00:00:00Z",
"System.State": "New",
"System.TeamProject": "sometext",
"System.Title": "sometext",
"System.Watermark": 1589040,
"System.WorkItemType": "Bug"
},
"id": 896092,
"relations": [
{
"attributes": {
"isLocked": false,
"name": "Related"
},
"rel": "System.LinkTypes.Related",
"url": "someurl"
},
{
"attributes": {
"authorizedDate": "2021-03-31T23:16:45.42Z",
"id": 12326470,
"name": "ParticleEffects.mp4",
"resourceCreatedDate": "2021-03-31T23:09:55.437Z",
"resourceModifiedDate": "2021-03-31T20:03:35.273Z",
"resourceSize": 23883076,
"revisedDate": "9999-01-01T00:00:00Z"
},
"rel": "AttachedFile",
"url": "someurl"
}
],
"rev": 2,
"url": "someurl"
}
My groovy knowledge is very basic so I tried with these 2 ways:
def getTags(){
def json = readJSON file: "test.json"
def tags = ""
def slurped = new JsonSlurper().parseText(json)
slurped.each{ key, value ->
value.each {k, v ->
println "${v}"
}
}
}
with which I get this error 15:30:31 hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (net.sf.json.JSONObject) values: [[fields:[AcornAgileProcess.BugSource:Ad Hoc, AcornAgileProcess.IssueType:Content, ...], ...]]
def getTags(){
def json = readJSON file: "test.json"
println json
def tags = ""
for (key in json.keySet()) {
if (key == "System.Tags"){
tags = json.get(key)
}
}
println tags
}
and with these one I do not know how to get deeper inside the fields value to get the ones inside of it
Solution
def json = readJSON file: "test.json"
println json.fields.'System.AreaPath'
it's the same as
def jsonString = readFile file: "test.json"
def json = new JsonSlurperClassic().parseText(jsonString)
println json.fields.'System.AreaPath'
JsonSlurperClassic returns serializable maps/arrays - use it to avoid non-serializable exception.
Answered By - daggett