<p>It looks like there might be a small issue with how the <strong>Extended Choice Parameter</strong> is set up.</p>
<p>Try this:</p>
<pre><code>pipeline {
    agent any

    parameters {
        extendedChoice(
            name: 'version_list',
            type: 'PT_CHECKBOX',
            groovyScript: '''
                import groovy.json.JsonSlurper

                def jsonFile = new File(&quot;${WORKSPACE}/input.json&quot;)
                def jsonContent = new JsonSlurper().parseText(jsonFile.text)
                return jsonContent.versions
            '''
        )
    }

    stages {
        stage(&quot;load json&quot;) {
            steps {
                echo &quot;version_list = ${params.version_list}&quot;
            }
        }
        stage(&quot;open_json&quot;) {
            steps {
                script {
                    def prop = readJSON file: &quot;${env.WORKSPACE}/input.json&quot;
                    def version_list = prop.versions

                    println version_list.join(',')
                }
            }
        }
    }
}

</code></pre>

CC BY-SA 4.0
2024-08-16T16:33:07.670Z
2024-08-16T16:33:07.670Z
YassineLbk
<p>I am completely novice at Jenkins,
this post <a href="https://devops.stackexchange.com/questions/11634/jenkins-pluginextended-choice-parameter-from-json-file">Jenkins Plugin:Extended Choice Parameter from JSON file</a>
does work for me.</p>
<p>I want to load a list of versions from a json file, which name is <strong>input.json</strong>,
and use this version list as choices of one parameter, named <strong>version_list</strong>.
The input.json file is in my <strong>$WORKSPACE</strong>, it's content is :</p>
<pre><code>{
    &quot;versions&quot;:[&quot;V1&quot;,&quot;V2&quot;]
}
</code></pre>
<p>The pipeline script is as follows:</p>
<pre><code>pipeline {
agent any


    stages {
        
        stage(&quot;load json&quot;) {
            steps {
                echo &quot;version_list = ${params.version_list}&quot;
            }
        }
        stage(&quot;open_json&quot;) {
            steps {
                script {
                    def prop = readJSON file: &quot;${env.WORKSPACE}/input.json&quot;
                    def version_list = prop.versions
    
                    println version_list.join(',')
                }
            }
        }
    }
}
</code></pre>
<p>The parameter <strong>version_list</strong>  is setted as follows:</p>
<ul>
<li><p>Extended Choice Paramter</p>
</li>
<li><p>name: versioin_list</p>
</li>
<li><p>Parameter Type: Check Boxes</p>
</li>
<li><p>Choose Source for Value: Groovy Script, and it's content is:</p>
</li>
</ul>
<pre><code>def prop = readJSON file: &quot;${env.WORKSPACE}/input.json&quot;
def version_list = prop.versions
return version_list.join(',')
</code></pre>
<p>When I build this job, there is nothing to choice for parameter version_list,
but the output shows it can read data from input.json, this is one output:</p>
<pre><code>Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /home/hdl/.jenkins/workspace/json_as_parameter
[Pipeline] {
[Pipeline] stage
[Pipeline] { (load json)
[Pipeline] echo
version_list = null
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (open_json)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
&quot;V1&quot;,&quot;V2&quot;
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
</code></pre>
<p>The load json stage can not get the value of ${params.version_list}
but the open_json stage can load json properly.</p>
<p>If is replace groovy script of versison_list simpley as follows:</p>
<pre><code>def version_list = [&quot;V1&quot;,&quot;V2&quot;]
return version_list.join(',')
</code></pre>
<p>It works well.</p>
<p>I don't know why.
Any help, thanks!</p>

<jenkins><json>
Jenkins loads data from json file as choices of parameter does not work properly
Mr.Bingo
# Jenkins loads data from json file as choices of parameter does not work properly

(Asked by: username_0 on 2024-03-18)

I am completely novice at Jenkins,
this post Jenkins Plugin:Extended Choice Parameter from JSON file
does work for me.
I want to load a list of versions from a json file, which name is input.json,
and use this version list as choices of one parameter, named version_list.
The input.json file is in my $WORKSPACE, it's content is :
{
    "versions":["V1","V2"]
}

The pipeline script is as follows:
pipeline {
agent any


    stages {
        
        stage("load json") {
            steps {
                echo "version_list = ${params.version_list}"
            }
        }
        stage("open_json") {
            steps {
                script {
                    def prop = readJSON file: "${env.WORKSPACE}/input.json"
                    def version_list = prop.versions
    
                    println version_list.join(',')
                }
            }
        }
    }
}

The parameter version_list  is setted as follows:

Extended Choice Paramter

name: versioin_list

Parameter Type: Check Boxes

Choose Source for Value: Groovy Script, and it's content is:


def prop = readJSON file: "${env.WORKSPACE}/input.json"
def version_list = prop.versions
return version_list.join(',')

When I build this job, there is nothing to choice for parameter version_list,
but the output shows it can read data from input.json, this is one output:
Started by user admin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /home/hdl/.jenkins/workspace/json_as_parameter
[Pipeline] {
[Pipeline] stage
[Pipeline] { (load json)
[Pipeline] echo
version_list = null
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (open_json)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
"V1","V2"
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

The load json stage can not get the value of ${params.version_list}
but the open_json stage can load json properly.
If is replace groovy script of versison_list simpley as follows:
def version_list = ["V1","V2"]
return version_list.join(',')

It works well.
I don't know why.
Any help, thanks!

**Tags:** jenkins, json


---

## 1 answers

### Answer by username_1 on 2024-08-16. Score: 0

It looks like there might be a small issue with how the Extended Choice Parameter is set up.
Try this:
pipeline {
    agent any

    parameters {
        extendedChoice(
            name: 'version_list',
            type: 'PT_CHECKBOX',
            groovyScript: '''
                import groovy.json.JsonSlurper

                def jsonFile = new File("${WORKSPACE}/input.json")
                def jsonContent = new JsonSlurper().parseText(jsonFile.text)
                return jsonContent.versions
            '''
        )
    }

    stages {
        stage("load json") {
            steps {
                echo "version_list = ${params.version_list}"
            }
        }
        stage("open_json") {
            steps {
                script {
                    def prop = readJSON file: "${env.WORKSPACE}/input.json"
                    def version_list = prop.versions

                    println version_list.join(',')
                }
            }
        }
    }
}


---

