0

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."
            ]
          }
        ]
venkat sai
  • 455
  • 9
  • 30

1 Answers1

1

Your code matches the JSON data you provided and should work fine. However looking into the error you're getting for some files the structure is different.

You can work it around by explicitly checking the type of subname you're getting using instanceof operator like:

names.each { name ->
    name.each { subName ->
        if (subName instanceof Map) {
            subName.get('given').each { givenName ->
                if (givenName != null)
                    givenList << givenName
            }
        } else if (subName instanceof String) {
            givenList << subName
        } else {
            log.error('Expected Map or String, got' + subName.getClass().getName())
        }


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

Demo:

enter image description here

Other improvements:

  1. You don't seem to be using JsonSlurper at all so you can just simplify this line:

    jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
    

    to this one jsonString = new File(pathToRead + "Data/fhir/" + index.getAt(random)).text

  2. This line will never visit the last value in the index.csv

    randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size-1);
    

    you should change it to

    randoms << org.apache.commons.lang3.RandomUtils.nextInt(0, size);
    
  3. You can put your code in a global try block and print the problematic JSON into jmeter.log file in case of failure

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133