2017-06-22 14 views
5

このJSONファイルはGroovyで作成する必要があります。 私は多くのもの(JsonOutput.toJson()/JsonSlurper.parseText())をうまく試してみました。Jenkins Pipeline Groovy json parsing

{ 
    "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 
      } 
     ] 
     } 
    ] 
} 

これはJenkinsのビルドメッセージをSlackに投稿するためのものです。

+0

質問のタイトルであなたはパッシングについて質問し、質問自体にjsonファイルの作成について質問します。あなたはあなたが何をしたいか/やろうとすることを明確にしてください。 – daggett

+0

@daggett JSONオブジェクトをgroovy変数に作成したいと思います。 –

答えて

10

JSONは、人間が判読可能なテキストを使用して、属性値ペアと配列データ型からなるデータオブジェクトを送信する形式です。 一般に、jsonは書式設定されたテキストです。

groovy jsonオブジェクトは単なるマップ/配列のシーケンスです。

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

建築JSONコードからパイプラインを使用してJSONを解析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}" 
} 

を使用して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) 

} 
5

私が何かをしようとしている間にこの質問が見つかりました(私は信じていました)は簡単なはずですが、もう1つの答えで対処されていませんでした。変数内に文字列としてJSONをロードしている場合、どのようにネイティブオブジェクトに変換しますか?もちろん、他の答えが示唆するように、あなたはnew JsonSlurperClassic().parseText(json) を行うことができますが、これを行うにはジェンキンスにおけるネイティブな方法があります:

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

が、これは誰かに役立ちます願っています。

編集:コメントで説明したように、「ネイティブ」はあまり正確ではありません。

+2

良い呼び出し、これはネイティブではありませんが、[pipeline utility steps plugin](https://plugins.jenkins.io/pipeline-utility-steps)が必要です。優れたプラグインを利用する [全文はこちら](https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/) –

関連する問題