从Jenkins管道中的Groovy变量创建JSON字符串

Mr_*_*TeD 17 groovy json jenkins jenkins-pipeline

我必须在Groovy中创建这个JSON文件.我尝试了很多东西(JsonOutput.toJson()/ JsonSlurper.parseText())但没有成功.

{
   "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
            }
         ]
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

这是为了向Slack发布Jenkins构建消息.

dag*_*ett 42

JSON是一种使用人类可读文本传输由属性 - 值对和数组数据类型组成的数据对象的格式.所以,一般来说json是一个格式化的文本.

在groovy中,json对象只是一系列地图/数组.

使用JsonSlurperClassic解析json

//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}"
}
Run Code Online (Sandbox Code Playgroud)

使用管道解析json

node{
    def data = readJSON file:'message2.json'
    echo "color: ${data.attachments[0].color}"
}
Run Code Online (Sandbox Code Playgroud)

从代码构建json并将其写入文件

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)

}
Run Code Online (Sandbox Code Playgroud)

  • 我正在尝试这样做。我收到“错误:org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException:不允许脚本使用新的groovy.json.JsonSlurperClassic”我缺少什么? (2认同)

mrf*_*red 17

在我尝试做某事时(我相信)发现这个问题应该很简单,但是其他答案没有解决.如果您已将JSON作为字符串加载到变量中,那么如何将其转换为本机对象?显然你可以new JsonSlurperClassic().parseText(json) 像其他答案所说的那样做,但詹金斯有一种本地的做法:

node () {
  def myJson = '{"version":"1.0.0"}';
  def myObject = readJSON text: myJson;
  echo myObject.version;
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人.

编辑:正如评论中所解释的"原生"不太准确.

  • 好的电话,虽然这不是原生的,但它需要[管道实用程序步骤插件](https://plugins.jenkins.io/pipeline-utility-steps).一个很好的插件,可以使用.[完整文档](https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/) (5认同)