-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjson-parse-pipeline.groovy
48 lines (40 loc) · 1.15 KB
/
json-parse-pipeline.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import groovy.json.JsonSlurperClassic
/**
* Parse JSON to a map.
*
* It needs script approvals:
*
* 'method groovy.json.JsonSlurperClassic parseText java.lang.String'
* 'new groovy.json.JsonSlurperClassic'
* 'new java.util.HashMap java.util.Map'
*
* Also see:
* http://stackoverflow.com/questions/37864542/jenkins-pipeline-notserializableexception-groovy-json-internal-lazymap
*
* @param json Json string to parse with name / value properties
* @return A map of properties
*/
@NonCPS
def parseJsonToMap(String json) {
final slurper = new JsonSlurperClassic()
return new HashMap<>(slurper.parseText(json))
}
pipeline {
agent any
stages {
stage('json') {
steps {
script {
def json = "{\n" +
" \"foo\":\"f00\",\n" +
" \"bar\":\"baa\"\n" +
"}"
echo "Parsing JSON: ${json}"
def map = parseJsonToMap(json)
echo "foo = ${map.foo}"
echo "bar = ${map.bar}"
}
}
}
}
}