jenkins pipeline脚本获取接口返回循环key

279 阅读1分钟

jenkins pipeline脚本获取接口返回循环key

pipeline {
    agent any
    stages {
        stage('HTTP Request') {
            steps {
                script {
                    // 调用HTTP GET接口
                    def response = httpRequest 'http://your-api-endpoint'

                    // 解析JSON响应
                    def jsonSlurper = new groovy.json.JsonSlurper()
                    def jsonResponse = jsonSlurper.parseText(response.content)

                    // 迭代循环key
                    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)")|.[]'
                    '''
                }
            }
        }
    }
}