Team,
I am making a call to gerrit server and return is I believe json. From this I am trying to read fields but getting error in the first step itself. any hint? My call to gerrit and storing its response is successful but further reading it is my challenge.
output is
Raw Response: )]}'
{"cherrypick":{"method":"POST","label":"Cherry Pick","title":"Cherry pick change to a different branch","enabled":true},"description":{"method":"PUT","label":"Edit Description","enabled":true},"rebase":{"method":"POST","label":"Rebase","title":"Rebase onto tip of branch or parent change.","enabled":true,"enabled_options":["rebase","rebase_on_behalf_of_uploader"]}}
Also: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: 49aaf903-ceee-4e7e-83d8-642316a37b95
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object
The current character read is ')' with an int value of 41
Unable to determine the current character, it is not a string, number, array, or object
line number 1
index number 0
)]}'
^
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:206)
at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:157)
my jenkinsfile is
stages {
stage('Read Json From Gerrit') {
steps {
script {
def response = sh(
script: "curl -u ${GERRIT_HTTP_USR}:${GERRIT_HTTP_PSW} -s '$GERRIT_API_URL_CHANGES/$GERRIT_CHANGE_NUMBER/revisions/$GERRIT_PATCHSET_REVISION/actions'",
returnStdout: true
).trim()
echo "Raw Response: ${response}"
def json = new JsonSlurper().parseText(response)
All I want to do is just see if enabled is true? this one
"enabled":true
2
Answers
The error you’re encountering is due to the extra characters at the beginning of the JSON response:
)]}'
. This is a common issue when dealing with JSONP responses or certain APIs that wrap their JSON responses.To fix the issue, you need to remove the unwanted characters before parsing the JSON. Here’s how you can modify:
Just in case you are curious why the response looks the way it does, here is a link to the documentation: