Jenkinsfile write JSON file

Viewed 16744

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?

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()))
        }

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()
)
Related