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?
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()))
}
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
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()
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With