1

Here is the snippet of my groovy script: jsonFileData = slurper.parse(jsonFile)

Here is my JSON file

{
    "MEMXYZ": {
        "LINKOPT": {
            "RMODE": "31",
            "AMODE": "ANY"
        },
        "PROCESSOR": "PROCESSOR XYZ",
        "DB2": {
            "OWNER": "USER1",
            "QUALIFER": "DB2ADMIN",
            "SSID": "DBC1"
        },
        "COBOL": {
            "VERSION": "V6",
            "CICS": "V5R6M0",
            "OPTIONS": "LIST,MAP,RENT",
            "DB2": "YES"
        }
    }
}
println "Print1 ***** Parsing PROCESSOR  = ${jsonFileData.MEMXYZ.PROCESSOR}"
println "Print2 ***** Parsing PROCESSOR  = ${jsonFileData}.${Member}.PROCESSOR"

The Print1 is working fine with with explicit Member name "MEMXYZ", but I have problem with Print2, which I need to have the dyanmic ${Member} variable substitution. Please help!

${Member} is MEMXYZ

Please help to solve the Print2 statement

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Ray Lam
  • 13
  • 3

1 Answers1

0

".. ${abc} ..." just injects the value of abc variable into string.

To access values of map (result of slurper.parse(...) in your case) you could use one of approaches:

jsonFileData[Member].PROCESSOR
jsonFileData[Member]['PROCESSOR']

So, your print line could look like:

println "PROCESSOR  = ${jsonFileData[Member].PROCESSOR}"
daggett
  • 26,404
  • 3
  • 40
  • 56
  • How to assign the parsed text to a variable, I got an error with the following statement: String parsedText = ${jsonFileData[Member].PROCESSOR} – Ray Lam Feb 21 '23 at 13:31
  • just remove `${}` - keep only expression between brackets. `String parsedText = jsonFileData[Member].PROCESSOR`. `${}` is only for string interpolation: `"...${a}..." == "..."+a+"..."` – daggett Feb 21 '23 at 13:46