Home > Enterprise >  How to populate the Active choice Parameter dropdown list in Jenkins using a JSON file
How to populate the Active choice Parameter dropdown list in Jenkins using a JSON file

Time:12-02

I am trying to use Active Choice Parameter Choice Parameter to populate the dropdown list from JSON values. However when I use the Build with Parameters its blank. I want to populate the FullName value in the dropdown list.

Screenshot

cust.json :

[ {
    "fullName": "Company Inc.",
    "shortName": "comp",
    "salesforceID": "0000",
    "jpower": "00000"
},{
    "fullName": "Company 1 Inc.",
    "shortName": "comp1",
    "salesforceID": "0001",
    "jpower": "00001"
}]

My Groovy script :

import groovy.json.JsonSlurperClassic

def file_data= new File('/home/jenkins/readJson/ds-github-utils/conf/customers.json').text
def jsonParser = new JsonSlurperClassic()
def data = jsonParser().parseText(file_data)
return fullName

CodePudding user response:

Your script has two errors, the is not method () on defined jsonParser object so the line: def data = jsonParser().parseText(file_data), must be: def data = jsonParser.parseText(file_data).

The second problem is that fullname is not a variable and is not defined in the context, if you want to access the json fullname value, you must use parsed object in data variable : return data.fullName.

All together (I put the json inside the script only to testing purposes but you can read from your file as you do in your script):

import groovy.json.JsonSlurper

def filedata = 
''' 
[ {
    "fullName": "Company Inc.",
    "shortName": "comp",
    "salesforceID": "0000",
    "jpower": "00000"
},{
    "fullName": "Company 1 Inc.",
    "shortName": "comp1",
    "salesforceID": "0001",
    "jpower": "00001"
}]
'''

def jsonParser = new JsonSlurper()
def data = jsonParser.parseText(filedata)
return data.fullName

This code returns [Company Inc., Company 1 Inc.]

  • Related