0

I'm trying to access each data from the json, for example, echo responde.text. This is the code

stage("Using curl example") {
        steps {
            script {
                final String url = "http://devrest01.ydilo.es:8080/yoigooq/text?text=hola"

                final response = sh(script: "curl -s $url", returnStdout: true)

                echo response

                //Here i want to access an specific data from the json
                
            }
        }
    }
Alejandro Sánchez
  • 69
  • 1
  • 3
  • 11
  • maybe use something like [jq](https://stedolan.github.io/jq/) but it requires that it is installed on jenkins nodes where you run your pipeline. Or [JsonSlurper](https://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html) ? – Michał Krzywański Aug 13 '21 at 07:30
  • Does this answer your question? [Parsing JSON on Jenkins Pipeline](https://stackoverflow.com/questions/55174084/parsing-json-on-jenkins-pipeline-groovy) – Noam Helmer Aug 13 '21 at 09:13

3 Answers3

1

The easiest way to achieve it is using the built in key word readJson, which is part of the Pipeline Utility Steps (which is usually installed by default):

readJSON- Reads a file in the current working directory or a String as a plain text JSON file. The returned object is a normal Map with String keys or a List of primitives or Map.

You can use it to read files from the workspace or to parse a given Json text, in both cases it will return a dictionary representation of the given json, which can be then used easily in the code.
In your case you it will look like:

stage("Using curl example") {
    steps {
        script {
            def url = "http://devrest01.ydilo.es:8080/yoigooq/text?text=hola"
            def response = sh(script: "curl -s $url", returnStdout: true)

            jsonData = readJSON text: response
            echo jsonData.text  // you can also use jsonData['text']
        }
    }
}
Noam Helmer
  • 5,310
  • 1
  • 9
  • 29
0

This can be done easily using the readJSON function, see the doc

stage("Using curl example") {
    steps {
        script {
            final String url = "http://devrest01.ydilo.es:8080/yoigooq/text?text=hola"
            final response = sh(script: "curl -s $url", returnStdout: true)
            echo response
            def jsonObj = readJSON text: response.join(" ")
            echo jsonObj                
        }
    }
}
uncletall
  • 6,609
  • 1
  • 27
  • 52
0

First, curling the json file, actually you can build a json map from a string or a file . You can use json slurper classic : import groovy.json.JsonSlurperClassic, if you want to use Files I/O , import groovy.io.FileType

let say this json file at your url

{
"key1":"value1", 
"array1":[{"array_key1":"value2"},{"array_key2":"value3"}]
}
import groovy.json.JsonSlurperClassic
import groovy.io.FileType   
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
                // curl your file let say locally 
               sh 'curl -o ${WORKSPACE}/jsonfile.json http://yourURL'
              def jsonObject = readJSON file: "${WORKSPACE}/jsonfile.json"
              // also: def jsonObject = readJSON( file: "${WORKSPACE}/jsonfile.json")

              echo "${jsonObject.key1}"
              if ( jsonObject.has("array1") {// you can test if element..
                    echo "${jsonObject.array_key1}" 
             }
            }
        }
    }
}
Aplantie
  • 15
  • 5