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)
}
質問のタイトルであなたはパッシングについて質問し、質問自体にjsonファイルの作成について質問します。あなたはあなたが何をしたいか/やろうとすることを明確にしてください。 – daggett
@daggett JSONオブジェクトをgroovy変数に作成したいと思います。 –