lundi 5 novembre 2018

Reading json file as json string and extracting data in jsr223 sampler, groovy as language in jmeter

import com.jayway.jsonpath.JsonPath

def pathToRead = someFilePath;
def pathToWrite = someOtherFilePath;

def newLine = System.getProperty('line.separator')
def index = [];
def randoms = [];
def temp;

//generating random numbers
def size = new File(pathToRead + "index.csv").readLines().size();
for(int i=0; i<vars.get('extractCount'); i++)
{
        randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
}

//Reading file names to extract data
File file = new File(pathToRead + "index.csv");
file.each { line ->
        index << line
}

def nameCSV = new File(pathToWrite + 'name.csv')
def nameGivenCSV = new File(pathToWrite + 'given.csv')
def givenList = []
def nameFamilyCSV = new File(pathToWrite + 'family.csv')
def familyList = []

//going through each json file and extracting data and storing in lists
randoms.unique().each { random ->
        jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
        def names = JsonPath.read(jsonString, '$..name')
        names.each { name ->
                name.each { subName ->
                        subName.get('given').each { givenName ->
                                if(givenName != null)
                                        givenList << givenName
                        }

                        temp = subName.get('family')
                        if(temp != null)
                                familyList << temp        
                }
        }       
}


//Writing data to files after removing the duplicates
givenList.unique().each { single_given -> 
                nameCSV << single_given << newLine
                nameGivenCSV << single_given << newLine
        }
        familyList.unique().each { single_family ->
                nameCSV << single_family << newLine
                nameFamilyCSV << single_family << newLine
        }

This is giving the error saying Problem in JSR223 script ExtractRandomData, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.get() is applicable for argument types: (java.lang.String) values: [given]

which means its not letting me apply subName.get('given');

the json data in file is like

"name": [
          {
            "use": "official",
            "family": "Cortez851",
            "given": [
              "Benito209"
            ],
            "prefix": [
              "Mr."
            ]
          }
        ]

Aucun commentaire:

Enregistrer un commentaire