0

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.

{
  "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 e.g. { "version": "2.6.5", "conf": "replian" }, { "version": "1.0.2", "conf": "hp" }, { "version": "1.0.2", "conf": "shutoff" }, { "version": "1.0.2", "conf": "spark" }

itgeek
  • 549
  • 1
  • 15
  • 33
  • Possible duplicate of [modifying json with jsonbuilder in Groovy](https://stackoverflow.com/questions/25748170/modifying-json-with-jsonbuilder-in-groovy) – Mostafa Hussein Mar 09 '19 at 09:17

3 Answers3

1

Using 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 '
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"
        }, ...
James Brown
  • 36,089
  • 7
  • 43
  • 59
0

I like to suggest groovy to do this.

Edited : See the // 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()
Bhanuchander Udhayakumar
  • 1,581
  • 1
  • 12
  • 30
  • groovy.json.JsonException: expecting a ',' or a ']', but got the current character of ':' with an int value of 58 on array index of 16 The current character read is ':' with an int value of 58 expecting a ',' or a ']', but got the current character of ':' with an int value of 58 on array index of 16 line number 67 index number 1524 "version": "0.0.0", ..................^ at groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:153) – itgeek Mar 11 '19 at 00:03
  • @itgeek Oops...! My mistake. Now i 've updated my code – Bhanuchander Udhayakumar Mar 11 '19 at 07:06
  • Thanks Bhanu, The value replaced first config "replian", and failed to replace version in other config – itgeek Mar 12 '19 at 01:42
  • @itgeek glad it helped – Bhanuchander Udhayakumar Mar 12 '19 at 05:55
  • I want to write them to a json file after substituting the values. Can I use the below one to write the parsed data to a json file and print the file content. writeFile file: 'outputfile.json', text: content println('Printing the output json file: ' + outputfile.json) – itgeek Mar 18 '19 at 13:42
  • @itgeek if you want in output you can add a new line as i 've updated in the last line of my answer. – Bhanuchander Udhayakumar Mar 19 '19 at 15:25
0

If ever you're interrested in a solution with sed : sed 's/"version": "1\.0\.2"/"version": "2.6.5"/ deploy.json