简体   繁体   English

如何使用Groovy替换json文件中的参数值

[英]How to substitute parameter value in a json file using Groovy

Is there any simple way to replace the version from " 1.0.2 " to " 2.6.5 " in a json file " deploy.json " using groovy scripting , and the file content has been provided below. 是否有任何简单的方法使用groovy脚本将json文件“ deploy.json ”中的版本从“ 1.0.2 ”替换为“ 2.6.5 ”,并且在下面提供了文件内容。

{
  "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"
        }
            ]
}

I tried the below, but getting an error; 我尝试了以下操作,但出现错误;

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

ISSUE: Only first conf version is replaced eg { "version": "2.6.5", "conf": "replian" }, { "version": "1.0.2", "conf": "hp" }, { "version": "1.0.2", "conf": "shutoff" }, { "version": "1.0.2", "conf": "spark" } 问题:仅替换第一个conf版本,例如{“ version”:“ 2.6.5”,“ conf”:“ replian”},{“ version”:“ 1.0.2”,“ conf”:“ hp”},{ “ version”:“ 1.0.2”,“ conf”:“关机”},{“ version”:“ 1.0.2”,“ conf”:“ spark”}

Using jq : 使用jq

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

Or awk, if you must: 或awk,如果您必须:

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

Some output: 一些输出:

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

I like to suggest groovy to do this. 我喜欢建议使用groovy做到这一点。

Edited : See the // Edited Line 编辑:请参见// 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()

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

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

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