【问题标题】:How to substitute parameter value in a json file using Groovy如何使用 Groovy 替换 json 文件中的参数值
【发布时间】:2019-03-09 02:45:28
【问题描述】:

有什么简单的方法可以将json文件“deploy.json1.0.2”替换为“2.6.5” /strong>" 使用 groovy 脚本,文件内容已在下面提供。

{
  "versions": [
        {
            "version": "1.0.2",
            "conf": "replian"
        },
        {
            "version": "1.0.2",
            "conf": "hp"
        },
        {
            "version": "1.0.2",
            "conf": "shutoff"
        },
        {
            "version": "1.0.2",
            "conf": "spark"
        }
            ]
}

我尝试了以下方法,但出现错误;

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def content = """
{
  "versions": [
        {
            "version": "1.0.2",
            "conf": "replian"
        },
        {
            "version": "1.0.2",
            "conf": "hp"
        },
        {
            "version": "1.0.2",
            "conf": "shutoff"
        },
        {
            "version": "1.0.2",
            "conf": "spark"
        }
            ]
}"""

def slurped = new JsonSlurper().parseText(content)
def builder = new JsonBuilder(slurped) 
builder.content.versions.find{it.version}.version = "2.6.5"
println(builder.toPrettyString())

问题: 仅替换第一个 conf 版本 例如 { “版本”:“2.6.5”, “conf”:“回复” }, { “版本”:“1.0.2”, “conf”:“hp” }, { “版本”:“1.0.2”, “conf”:“关闭” }, { “版本”:“1.0.2”, “conf”:“火花” }

【问题讨论】:

标签: json jenkins groovy jenkins-pipeline


【解决方案1】:

使用jq

$ jq '.versions[].version="2.6.5"' deploy.json
{
  "app": "Beach",
  "Process": "steam",
  "versions": [
    {
      "version": "2.6.5",
      "conf": "replian"
    }, ...

或者 awk,如果必须的话:

$ awk '
BEGIN {
    FPAT="([^:]*)|(\"[^\"]+\")"
    OFS=":"
}
$1~"\"version\"" {
    sub(/"[^"]*"/,"\"2.6.5\"",$2)
}1' deploy.json

一些输出:

{
  "app": "Beach",
  "Process": "steam",
  "versions": [
        {
            "version": "2.6.5",
            "conf": "replian"
        }, ...

【讨论】:

    【解决方案2】:

    如果您对 sed 的解决方案感兴趣:sed 's/"version": "1\.0\.2"/"version": "2.6.5"/ deploy.json

    【讨论】:

      【解决方案3】:

      我喜欢建议groovy 这样做。

      已编辑:// Edited Line

      import groovy.json.JsonBuilder
      import groovy.json.JsonSlurper
      
      def content = """
      {
        "app": "Beach",
        "Process": "steam",
        "versions": [
              {
                  "version": "1.0.2",
                  "conf": "replian"
              },
              {
                  "version": "1.0.2",
                  "conf": "hp"
              },
              {
                  "version": "1.0.2",
                  "conf": "shutoff"
              },
              {
                  "version": "1.0.2",
                  "conf": "spark"
              }
                  ]
      }"""
      
      def slurped = new JsonSlurper().parseText(content)
      def builder = new JsonBuilder(slurped) 
      builder.content.versions.find{it.version}.version = "2.6.5" // Edited Line
      println(builder.toPrettyString())
      
      // Updated Line
      new File ("out.json").text = builder.toPrettyString()
      

      【讨论】:

      • groovy.json.JsonException: 期待 ',' 或 ']',但在数组索引 16 上获得了 ':' 的当前字符,int 值为 58 当前读取的字符是':' 的 int 值为 58,预期为 ',' 或 ']',但在 16 行号 67 索引号 1524“版本”的数组索引上获得了 ':' 的当前字符,其 int 值为 58: "0.0.0", ..................^ 在 groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:153)
      • @itgeek 哎呀...!我的错。现在我已经更新了我的代码
      • 感谢 Bhanu,该值替换了第一个配置“replian”,并且无法替换其他配置中的版本
      • @itgeek 很高兴它有帮助
      • 我想在替换值后将它们写入 json 文件。我可以使用下面的方法将解析后的数据写入 json 文件并打印文件内容。 writeFile file: 'outputfile.json', text: content println('打印输出json文件: ' + outputfile.json)
      猜你喜欢
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多