I am trying to parse a text file with json and get one of the element from jsonArray. Below is the json i am trying to parse
[
{
"ContainerConfig": {
"Labels": {
"commit-id": "abcdef123d",
"author": "Jon"
}
}
}
]
Below is my groovy implementation in jenkinsfile
def jsonStr=readFile('temp.txt').trim()
//here temp.txt consist of above json
JsonSlurper slurper = new JsonSlurper()
def parsedJson=slurper.parseText(jsonStr)
def commitId=parsedJson[0].ContainerConfig.Labels.commit-id
I am getting this errorMsg -
java.lang.ClassCastException: org.jenkinsci.plugins.workflow.steps.EchoStep.message expects class java.lang.String but received class java.util.ArrayList
Using the JsonSlurper is not really best practice and can cause problems with CPS, use readJSON instead (which is also easier to use IMO).
I also suspect that the
-
incommit-id
causes the error and you should the["commit-id"]
snytax instead.