Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Json file with jenkins pipeline

I tried to display the json file with jenkins pipeline but i did not get to display the correct file format Evry time when i made a build, i found that the file format was changed

file suivi.installation.json

{
        "receptions": [{
                "loginacteur": "zex9450",
                "codeapplication": "SDD",
                "version": "1.0",
                "datereception": "2019-08-01",
                "numtran": 15000,
                "lien": "",
                "datepriseencompte": "2019-08-01",
                "composants": [{
                                "composant": "Gestion sapp ear",
                                "version": "1.0.8",
                                "technologie": "JBOSS",
                                "installations": [{
                                        "environnement": "SDD QF",
                                        "modedeploiement": "AUTO (JENKINS)",
                                        "dateinstallation": "2019-07-31",
                                        "loginacteur": "zex9450"
                                }]
                        }

                ]
        }]
}

file JenkinsFile

stage('Publication dans Suivi Installation CNI') {
    agent {label 'Jenkins-Pega-Master'}
    steps {
         script {

             def json = readJSON file: './output/suivi_installation.json'
                    echo "${json}"
                }      
            }
        }

Console Output

[Pipeline] withEnv
[Pipeline] {
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
[receptions:[[loginacteur:zex9450, codeapplication:SDD, version:1.0, datereception:2019-08-01, numtran:15000, lien:, datepriseencompte:2019-08-01, composants:[[composant:Gestion sapp ear, version:1.0.8, technologie:JBOSS, installations:[[environnement:SDD QF, modedeploiement:AUTO (JENKINS), dateinstallation:2019-07-31, loginacteur:zex9450]]]]]]]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
like image 820
Mhd Amine Avatar asked Oct 18 '25 14:10

Mhd Amine


2 Answers

You can use the JsonOutput class for pretty printing, but you first need to convert the format to be able to do so (this is already mentioned). An alternative:

import groovy.json.JsonOutput
....
steps {
     script {

         def json = readJSON file: './output/suivi_installation.json'
         def jsonFormat = JsonOutput.toJson(json)
         prettyJSON = JsonOutput.prettyPrint(jsonFormat)
                echo "${prettyJSON}"
            }      
        }

output in console:

{
    "receptions": [
        {
            "loginacteur": "zex9450",
            "codeapplication": "SDD",
            "version": "1.0",
            "datereception": "2019-08-01",
            "numtran": 15000,
            "lien": "",
            "datepriseencompte": "2019-08-01",
            "composants": [
                {
                    "composant": "Gestion sapp ear",
                    "version": "1.0.8",
                    "technologie": "JBOSS",
                    "installations": [
                        {
                            "environnement": "SDD QF",
                            "modedeploiement": "AUTO (JENKINS)",
                            "dateinstallation": "2019-07-31",
                            "loginacteur": "zex9450"
                        }
                    ]
                }
            ]
        }
    ]
}
like image 101
Unforgettable631 Avatar answered Oct 21 '25 07:10

Unforgettable631


Use JsonOutput library

import groovy.json.JsonOutput
...
prettyJSON = JsonOutput.prettyPrint(json)
echo("${prettyJSON}")
like image 36
metalisticpain Avatar answered Oct 21 '25 08:10

metalisticpain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!