jenkins pipeline脚本获取接口返回循环key
pipeline {
agent any
stages {
stage('HTTP Request') {
steps {
script {
def response = httpRequest 'http://your-api-endpoint'
def jsonSlurper = new groovy.json.JsonSlurper()
def jsonResponse = jsonSlurper.parseText(response.content)
jsonResponse.each { key, value ->
echo "Key: ${key}, Value: ${value}"
}
}
}
}
}
}
Jq方法
pipeline {
agent any
stages {
stage('HTTP Request') {
steps {
script {
// 使用curl发出HTTP请求,然后使用jq处理JSON响应
sh '''
curl -s "http://your-api-endpoint" | jq -c 'to_entries|map("\(.key):\(.value)")|.[]'
'''
}
}
}
}
}