Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile write JSON file

From within my Jenkinsfile, I am trying to create and write a simple JSON file to the workspace folder.

The contents of the JSON file should be:

{"people": {"name":"john","surname":"doe"}}

Any ideas?

like image 321
JoP Avatar asked Oct 31 '18 08:10

JoP


3 Answers

Got it!

script {
          def someMap = [
              'name' : "john",
              'surname' : "doe"
          ]
          def json = new groovy.json.JsonBuilder()
          json "people": someMap
          def file = new File("$WORKSPACE/people.json")
          file.write(groovy.json.JsonOutput.prettyPrint(json.toString()))
        }
like image 160
JoP Avatar answered Sep 21 '22 16:09

JoP


You can use writeJSON: Write JSON to a file in the workspace.

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#writejson-write-json-to-a-file-in-the-workspace

like image 21
Eugene Mihaylin Avatar answered Sep 22 '22 16:09

Eugene Mihaylin


If you don't want to use any plugin, there is a workaround with the core Jenkins method writeFile e.g.:

writeFile(
    file: "foo/bar.json",
    text: """\
        {
            'a': 'x',
            'b': 'y'
        }
    """.stripIndent()
)
like image 41
poerror Avatar answered Sep 21 '22 16:09

poerror