Search code examples
jsonjenkinsgroovyyamljenkins-pipeline

yaml to json with Groovy


I have a yaml file on a Jenkins pipeline (scripted in Groovy) and I want to convert that yaml file to JSON format to be parsed in that format.

I defined a variable (data) that will contain all the yaml file content. Don't know if that makes it easier to convert or not, but otherwise I can just convert the yaml file without putting the content on a variable.

Groovy stage script:

stage ("GET deployConfig file"){
    def data = readYaml file: './evaluations/integration-test- 
    docker/dev/deployConfig.yaml'
    println("YAML-FILE: " + data)     
}

Does anyone know how can achieve that?

thanks


Solution

  • Convert to json and write it to file

    import groovy.json.*
    
    stage{
        def data = readYaml file: ....
        def json = new JsonBuilder(data).toPrettyString()
        writeFile file: ..., text: json
    }