title: Reading json file as json string and extracting data in jsr223 sampler, groovy as language in jmeter
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>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&lt;vars.get('extractCount'); i++)
  {
   randoms &lt;&lt; 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 -&gt;
   index &lt;&lt; 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 -&gt;
   jsonString = new groovy.json.JsonSlurper().parseText(new File(pathToRead + "Data/fhir/" + index.getAt(random)).text);
   def names = JsonPath.read(jsonString, '$..name')
   names.each { name -&gt;
    name.each { subName -&gt;
     subName.get('given').each { givenName -&gt;
      if(givenName != null)
       givenList &lt;&lt; givenName
     }
  
     temp = subName.get('family')
     if(temp != null)
      familyList &lt;&lt; temp        
    }
   } 
  }
  
  
  //Writing data to files after removing the duplicates
  givenList.unique().each { single_given -&gt; 
    nameCSV &lt;&lt; single_given &lt;&lt; newLine
    nameGivenCSV &lt;&lt; single_given &lt;&lt; newLine
   }
   familyList.unique().each { single_family -&gt;
    nameCSV &lt;&lt; single_family &lt;&lt; newLine
    nameFamilyCSV &lt;&lt; single_family &lt;&lt; newLine
   }</code></pre>
  </div>
  </div>
  </p>
  
  <p>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]</p>
  
  <p>which means its not letting me apply subName.get('given');</p>
  
  <p>the json data in file is like</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>"name": [
            {
              "use": "official",
              "family": "Cortez851",
              "given": [
                "Benito209"
              ],
              "prefix": [
                "Mr."
              ]
            }
          ]</code></pre>
  </div>
  </div>
  </p>