Jenkins Groovy parse json

18,115

Solution 1

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'])

Solution 2

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.

Share:
18,115
Basti
Author by

Basti

Updated on July 01, 2022

Comments

  • Basti
    Basti almost 2 years

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