I got the following JSON:
{
"dev": {
"hoster": "123",
"id": "123",
},
"stage": {
"hoster": "123",
"id": "123",
"merge": "dev",
"slackChannel": "#dg-test-deployments"
},
"master": {
"hoster": "123",
"id": "123",
"merge": "stage",
},
"updates": {
"hoster": "123",
"id": "123",
"merge": "master",
"slackChannel": "#dg-test-deployments"
}
}
And want to check if the keys dev, stage, master and updates exists.
Any advice how to do that in groovy ? :)
You can try e.g.:
import groovy.json.JsonSlurper
def json = '''{
"dev": {
"hoster": "123",
"id": "123",
},
"stage": {
"hoster": "123",
"id": "123",
"merge": "dev",
"slackChannel": "#dg-test-deployments"
},
"master": {
"hoster": "123",
"id": "123",
"merge": "stage",
},
"updates": {
"hoster": "123",
"id": "123",
"merge": "master",
"slackChannel": "#dg-test-deployments"
}
}'''
def slurped = new JsonSlurper().parseText(json)
assert slurped.keySet().containsAll(['dev', 'stage', 'master', 'updates'])
Pipeline supports readJSON
and writeJSON
now.
Note: plugin Pipeline Utility Steps Plugin needs to be installed. See this answer for more info. Sample code can be found at github sample.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With