Skip to content

Instantly share code, notes, and snippets.

@t3knoid
Created February 14, 2018 17:22
Show Gist options
  • Save t3knoid/c0ececec1aa771b94c93018f258b5c0b to your computer and use it in GitHub Desktop.
Save t3knoid/c0ececec1aa771b94c93018f258b5c0b to your computer and use it in GitHub Desktop.
Jenkins Groovy method to parse JSON text
import groovy.json.JsonSlurper
/***
* This method uses JsonSlurper to parse a given JSON text into
* a more accessible data list and map structure. The following
* example illustrates this transformation
*
* def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
*
* assert result.person.name == "Guillaume"
* assert result.person.age == 33
* assert result.person.pets.size() == 2
* assert result.person.pets[0] == "dog"
* assert result.person.pets[1] == "cat"
*
* Mor details here http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/json/JsonSlurper.html.
*
* @param text is the JSON text that will be parsed
* returns a structured output
*
def parseJSON(text)
{
def slurper = new JsonSlurper()
def result = slurper.parseText(text)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment