Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create JSON strings from Groovy variables in Jenkins Pipeline

I have to create this JSON file in Groovy. I have try many things (JsonOutput.toJson() / JsonSlurper.parseText()) unsuccessfully.

{    "attachments":[       {          "fallback":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",          "pretext":"New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",          "color":"#D00000",          "fields":[             {                "title":"Notes",                "value":"This is much easier than I thought it would be.",                "short":false             }          ]       }    ] } 

This is for posting a Jenkins build message to Slack.

like image 201
Mr_DeLeTeD Avatar asked Jun 22 '17 18:06

Mr_DeLeTeD


People also ask

What is Groovy JSON JsonSlurperClassic?

public class JsonSlurperClassic. This is the original slurper included in case someone relies on its exact behavior. JSON slurper which parses text or reader content into a data structure of lists and maps. Example usage: def slurper = new groovy. json.

How do I declare a variable in Jenkins Groovy?

Jenkins Pipeline: How to Define a Variable – Jenkins Variables. Variables in a Jenkinsfile can be defined by using the def keyword. Such variables should be defined before the pipeline block starts. When variable is defined, it can be called from the Jenkins declarative pipeline using ${...} syntax.


2 Answers

JSON is a format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types. So, in general json is a formatted text.

In groovy json object is just a sequence of maps/arrays.

parsing json using JsonSlurperClassic

//use JsonSlurperClassic because it produces HashMap that could be serialized by pipeline import groovy.json.JsonSlurperClassic  node{     def json = readFile(file:'message2.json')     def data = new JsonSlurperClassic().parseText(json)     echo "color: ${data.attachments[0].color}" } 

parsing json using pipeline

node{     def data = readJSON file:'message2.json'     echo "color: ${data.attachments[0].color}" } 

building json from code and write it to file

import groovy.json.JsonOutput node{     //to create json declare a sequence of maps/arrays in groovy     //here is the data according to your sample     def data = [         attachments:[             [                 fallback: "New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",                 pretext : "New open task [Urgent]: <http://url_to_task|Test out Slack message attachments>",                 color   : "#D00000",                 fields  :[                     [                         title: "Notes",                         value: "This is much easier than I thought it would be.",                         short: false                     ]                 ]             ]         ]     ]     //two alternatives to write      //native pipeline step:     writeJSON(file: 'message1.json', json: data)      //but if writeJSON not supported by your version:     //convert maps/arrays to json formatted string     def json = JsonOutput.toJson(data)     //if you need pretty print (multiline) json     json = JsonOutput.prettyPrint(json)      //put string into the file:     writeFile(file:'message2.json', text: json)  } 
like image 124
daggett Avatar answered Sep 18 '22 11:09

daggett


Found this question while I was trying to do something (I believed) should be simple to do, but wasn't addressed by the other answer. If you already have the JSON loaded as a string inside a variable, how do you convert it to a native object? Obviously you could do new JsonSlurperClassic().parseText(json) as the other answer suggests, but there is a native way in Jenkins to do this:

node () {   def myJson = '{"version":"1.0.0"}';   def myObject = readJSON text: myJson;   echo myObject.version; } 

Hope this helps someone.

Edit: As explained in the comments "native" isn't quite accurate.

like image 36
mrfred Avatar answered Sep 20 '22 11:09

mrfred