Krishnaveni
Krishnaveni

Reputation: 33

How to find datatype of a variable in groovy

Below is my groovy code where I am framing a command using json data, but json data have different types data like list in array, or only single variable so can any one please tell me how to find data type of a variable.

Below code I have high-lighted the one element "jsondata[key]" that is my values of keys, and I want to check the data type of this values like in my JSON data i have params(key) with 4 list of arrays(values) so before using params values i have check the data type.

my expected result:

key : [1 2 3]

if (typeof(key) == list) {
    for (value in key) {
        #here i want to frame a command using previous keys..
    } 
}

like typeof() in python

import groovy.json.JsonSlurper

def label = "test testname params"

File jsonFile = newFile("/home/developer/Desktop/kramdeni/vars/PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
println jsondata.keySet()
println "jsondata: " + jsondata

def command = ""
keys = label.split(" ")
println "keys: " + keys

for (key in keys) {
    command += "-" + key + " " + **jsondata[key]** + " "
}
println "command: " + command

my json data:

{
    "test": "iTEST",
    "testname": "BOV-VDSL-link-Rateprofile-CLI-Test-1",
    "params": [
        {
            "n2x_variables/config_file": "C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP  Regression/BOV/Bov-data-1-single-rate-profile.xml"
        },
        {
            "n2x_variables/port_list": "303/4 303/1"
        },
        {
            "n2x_variables/port_list": "302/3 303/4"
        },
        {
            "n2x_variables/port_list": "301/3 303/5"
        }
    ]
}

Upvotes: 1

Views: 12403

Answers (2)

Michael Easter
Michael Easter

Reputation: 24498

The following code illustrates instanceof for String and List types:

import groovy.json.JsonSlurper

def label = "test testname params"
def jsonFile = new File("PARAMS.json")
def jsondata = new JsonSlurper().parse(jsonFile)
def command = ""
def keys = label.split(" ")

for (key in keys) {
    def value = jsondata[key]

    if (value instanceof String) {
        println "${key} ${value}"
    } else if (value instanceof List) {
        value.each { item ->
            println "${key} contains ${item}"
        }
    } else {
        println "WARN: unknown data type"
    }
}

example output for the JSON specified (I'm not sure how you want to build the command, so this is simple output. It should be easy to build up command as desired):

$ groovy Example.groovy 
test iTEST
testname BOV-VDSL-link-Rateprofile-CLI-Test-1
params contains [n2x_variables/config_file:C:/Program Files (x86)/Agilent/N2X/RouterTester900/UserData/config/7.30 EA SP1 Release/OSP  Regression/BOV/Bov-data-1-single-rate-profile.xml]
params contains [n2x_variables/port_list:303/4 303/1]
params contains [n2x_variables/port_list:302/3 303/4]
params contains [n2x_variables/port_list:301/3 303/5]

Upvotes: 1

Evgeny Smirnov
Evgeny Smirnov

Reputation: 3026

jsondata.each{ entry->
   println entry.value.getClass()
}

Upvotes: 6

Related Questions