简体   繁体   中英

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

You can try eg:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM