looking for some solutions? You are welcome.

SOLVED: How to substitute parameter value in a json file using Groovy

itgeek:

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.

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

I tried the below, but getting an error;

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.version = "2.6.5"
println(builder.toPrettyString())

Error:

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": "1.0.2", ..................^ at groovy.json.internal.JsonParserCharArray.complain(JsonParserCharArray.java:153) at groovy.json.internal.JsonParserCharArray.decodeJsonArray(JsonParserCharArray.java:356) at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:182)



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent