Created
February 14, 2018 17:22
-
-
Save t3knoid/c0ececec1aa771b94c93018f258b5c0b to your computer and use it in GitHub Desktop.
Jenkins Groovy method to parse JSON text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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