skip to Main Content

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


  1. 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:

    import groovy.json.JsonSlurper
    
    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}"
    
                    // Remove the unwanted characters at the start
                    def jsonResponse = response.replaceAll(/^)]}'.*/, '')
    
                    // Parse the cleaned JSON response
                    def json = new JsonSlurper().parseText(jsonResponse)
    
                    // Check if 'enabled' is true
                    def isEnabled = json.rebase?.enabled
                    echo "Is Enabled: ${isEnabled}"
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. Just in case you are curious why the response looks the way it does, here is a link to the documentation:

    To prevent against Cross Site Script Inclusion (XSSI) attacks, the JSON response body starts with a magic prefix line that must be stripped before feeding the rest of the response body to a JSON parser:

      )]}'
      [ ... valid JSON ... ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search