You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用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

问题排查与修复方案

问题点:

  1. JsonSlurper调用错误:jsonParser().parseText(file_data)多了一对括号,应该直接用jsonParser.parseText(file_data)
  2. 未提取目标字段:直接返回未定义的fullName变量,没有遍历JSON数组提取每个对象的fullName属性
  3. 权限问题(需额外确认):确保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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 05:40:28