简体   繁体   English

使用 Groovy 将 yaml 转换为 json

[英]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.我在 Jenkins 管道上有一个 yaml 文件(用 Groovy 编写),我想将该 yaml 文件转换为 JSON 格式,以便以该格式进行解析。

I defined a variable (data) that will contain all the yaml file content.我定义了一个包含所有 yaml 文件内容的变量(数据)。 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.不知道这是否使转换更容易,但否则我可以只转换 yaml 文件而不将内容放在变量上。

Groovy stage script: Groovy 阶段脚本:

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谢谢

Convert to json and write it to file转换为json并写入文件

import groovy.json.*

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM