1

I am trying to use the Jenkins pipeline for Json format input. I am trying to pass the Json format String in multiline string parameter where I am passing parameter like below image but I am not able to able to parse it using JsonSlurper, where test is the name of the multiline string input. Can anyone help me how can I parse it in groovy?

Is it possible to parse the Json format in multiline string parameter ?

multiline string parameter input as Json

def jsonSlurper = new JsonSlurper();
def infra = jsonSlurper.parse($test) ;
0

1 Answer 1

1

You don't have to use JsonSulper for this, you can use readJson step instead. Here is a complete sample.

pipeline {
  agent any
  parameters { string(name: 'JSON', defaultValue: 'staging', description: '') }
  stages {
      stage ("Example") {
        steps {
         script{
            def jsonObj = readJSON text: "${params.JSON}"
            echo "$jsonObj"
        } }
      }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Can you also tell how to access values from jsonObj?
@AnujSharma This depends on the structure of your JSON. There are bunch of examples here jenkins.io/doc/pipeline/steps/pipeline-utility-steps/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.