如何用JSON文件填充Jenkins的Active Choice Parameter下拉列表
问题:Jenkins Active Choice Parameter 无法从JSON文件读取fullName填充下拉列表
尝试使用Jenkins的Active Choice Parameter从JSON文件读取值填充下拉列表,但点击“Build with Parameters”时下拉列表为空,希望将JSON中的fullName值填充到下拉列表中。
提供的JSON文件内容(cust.json)
[ { "fullName": "Company Inc.", "shortName": "comp", "salesforceID": "0000", "jpower": "00000" },{ "fullName": "Company 1 Inc.", "shortName": "comp1", "salesforceID": "0001", "jpower": "00001" }]
原Groovy脚本
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
问题排查与修复方案
问题点:
- JsonSlurper调用错误:
jsonParser().parseText(file_data)多了一对括号,应该直接用jsonParser.parseText(file_data) - 未提取目标字段:直接返回未定义的
fullName变量,没有遍历JSON数组提取每个对象的fullName属性 - 权限问题(需额外确认):确保Jenkins进程拥有读取指定JSON文件路径的权限
修正后的Groovy脚本
import groovy.json.JsonSlurperClassic // 读取JSON文件内容 def fileData = new File('/home/jenkins/readJson/ds-github-utils/conf/customers.json').text // 初始化JSON解析器 def jsonSlurper = new JsonSlurperClassic() // 解析JSON字符串为数组对象 def customerList = jsonSlurper.parseText(fileData) // 遍历数组,提取所有fullName字段并返回 return customerList.collect { it.fullName }
内容的提问来源于stack exchange,提问作者NagalaeVik
相关产品推荐
相关产品推荐

