2017-06-22 5 views
5

Ich muss diese JSON-Datei in Groovy erstellen. Ich habe viele Dinge (JsonOutput.toJson()/JsonSlurper.parseText()) erfolglos versucht.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 
      } 
     ] 
     } 
    ] 
} 

Dies ist eine Jenkins bauen Nachricht für die Buchung an Slack.

+0

in Titel der Frage, die Sie über das Parsen fragen, und in Frage stellen Sie sich Fragen über das Erstellen von JSON-Datei. Könnten Sie bitte klarstellen, was Sie wollen/versuchen zu tun? – daggett

+0

@daggett Ich möchte dieses JSON-Objekt in eine groovige Variable erstellen. –

Antwort

10

JSON ist ein Format, das lesbaren Text verwendet, um Datenobjekte zu übertragen, die aus Attribut-Wert-Paaren und Array-Datentypen bestehen. Also, im Allgemeinen ist JSON ein formatierter Text.

In groovy JSON-Objekt ist nur eine Sequenz von Karten/Arrays.

Parsen json JsonSlurperClassic mit

//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 Pipeline

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

Gebäude json von Code Parsen und schreiben Sie es

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) 

} 
Datei
5

Diese Frage gefunden, während ich versuchte, etwas zu tun (ich glaubte), sollte einfach zu tun sein, wurde aber von der anderen Antwort nicht angesprochen. Wenn Sie den JSON bereits als String innerhalb einer Variablen geladen haben, wie konvertiert man ihn in ein natives Objekt? Offensichtlich könnten Sie tun new JsonSlurperClassic().parseText(json) als die andere Antwort schon sagt, aber es ist eine native Weg in Jenkins, dies zu tun:

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

Hope this jemand hilft.

Edit: Wie in den Kommentaren erklärt "nativ" ist nicht ganz genau.

+2

Guter Aufruf, obwohl dies nicht ganz nativ ist, erfordert es das [Plugin für Pipeline-Dienstprogramme] (https://plugins.jenkins.io/pipeline-utility-steps). Ein exzellentes Plugin, um es zu nutzen. [Vollständige Dokumentation hier] (https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/) –

Verwandte Themen