Parsing JSON on Jenkins Pipeline (groovy) Parsing JSON on Jenkins Pipeline (groovy) jenkins jenkins

Parsing JSON on Jenkins Pipeline (groovy)


I finally find a BETTER solution!

readJSON() method from the Jenkins "Pipeline Utility Steps" plugin as shown here:

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace

Here is a sample where we can finally ditch that ugly GROOVY JSONPARSE crap.

node() {    stage("checkout") {        def jsonString = '{"name":"katone","age":5}'        def jsonObj = readJSON text: jsonString        assert jsonObj['name'] == 'katone'  // this is a comparison.  It returns true        sh "echo ${jsonObj.name}"  // prints out katone        sh "echo ${jsonObj.age}"   // prints out 5    }}