wanderors
wanderors

Reputation: 2292

How to get the values from a groovy output in jenkins pipeline

My one output from jenkins groovy script is :

{
  "Code" : "Success",
  "LastUpdated" : "2021-08-31T18:12:23Z",
  "Type" : "sample",
  "AccessKeyId" : "reallysample",
  "SecretAccessKey" : "valuesomerandom",
  "Token" : "hugewrong+data=",
  "Expiration" : "2021-09-01T00:48:09Z"
}

How can I extratc the values for - AccessKeyId and SecretAccessKey from this. Please help, currently I am blocked

Upvotes: 0

Views: 417

Answers (1)

Noam Helmer
Noam Helmer

Reputation: 6824

You can use the readJSON keyword which is part of the Pipeline Utility Steps Jenkins plugin.

Reads a file in the current working directory or a String as a plain text JSON file. The returned object is a normal Map with String keys or a List of primitives or Map.

So you can use the readJSON to convert your JSON output into a dictionary that will allow easy access to all variables.
Here is an example (assuming you output is inside the output variable):

def props = readJSON text: output // assuming output contains your JSON text

println "AccessKeyId is: ${props.AccessKeyId}"  // you can also use props['AccessKeyId']
println "SecretAccessKey is: ${props.SecretAccessKey}"

Upvotes: 1

Related Questions