Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Groovy parse json

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 ? :)

like image 475
Basti Avatar asked Mar 02 '17 08:03

Basti


2 Answers

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'])
like image 146
Opal Avatar answered Oct 29 '22 09:10

Opal


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.

like image 21
akostadinov Avatar answered Oct 29 '22 08:10

akostadinov