Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-69705

[readJSON] Invalid JSON String when file starts with spaces (ok with text param)

XMLWordPrintable

      I used readJSON to parse a JSON string. But after migrated content to a file, my pipline stoped on:

      hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String
      	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:143)
      	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)
      	at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:84)
      	at org.jenkinsci.plugins.pipeline.utility.steps.json.ReadJSONStepExecution.doRun(ReadJSONStepExecution.java:78)
      	at org.jenkinsci.plugins.pipeline.utility.steps.AbstractFileOrTextStepExecution.run(AbstractFileOrTextStepExecution.java:29)
      	at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
      	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
      	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
      	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      	at java.base/java.lang.Thread.run(Thread.java:829)
      

      After several die&retry, I found out issue come from the leading spaces at file start. MCVE:

      def text = ' {}'
                          
      pipeline {
          agent any
          
          stages {
              stage('text') {
                  steps {
                      script {
                          def json = readJSON text: text // OK
                          println json
                      }
                  }
              }
              stage('file') {
                  steps {
                      script {
                          writeFile file:'inputs.json', text: text, encoding: 'UTF-8'
                          sh 'cat inputs.json'
                          def json = readJSON file: 'inputs.json' // ERROR
                          println json
                      }
                  }
              }
          }
      }
      

            rsandell rsandell
            loganmzz Logan Mzz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: