jsongroovyjsonslurper

Groovy slurper.parser variable of a variable


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


Solution

  • ".. ${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}"