Search code examples
jsonjmeterjmeter-5.0

Need help in extracting values from Json for Jmeter


My Json is like below, I want to extract json for all the "code" values and put them with comma separated. **I have almost 250 code values and want them like this

RFI027,RFI037,RFI407,RFI055,RFI457,RFI677,RFI068,RFI086

{
   "totalDocs":202,
   "recordBatchSize":224,
   "listingType":31,
   "currentPageNo":1,
   "recordStartFrom":18,
   "columnHeader":[
      {
         "id":"0",
         "fieldName":"commId",
         "isCustomAttributeColumn":false,
         "isActive":false
      },
      {
         "id":"24264704",
         "function":"",
         "funParams":"",
         "wrapData":"",
      },
       {
         "code":"RFI027",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI037",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI407",
         "noOfActions":0,
         "observationId":0         
      },      
      {
         "code":"RFI055",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI457",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI677",
         "noOfActions":0,
         "observationId":0         
      },
      {
         "code":"RFI068",
         "noOfActions":0,
         "observationId":0         
      },      
      {
         "code":"RFI086",
         "noOfActions":0,
         "observationId":0         
      },
   ],
   "sortField":"updated",
   "sortFieldType":"timestamp",
   "sortOrder":"desc",
   "editable":true,
   "isIncludeSubFolder":true,
   "totalListData":0
}

I tried with $..code in Jmeter Json Extractor but it returns only one Value. but I want output like RFI027,RFI037,RFI407,RFI055,RFI457,RFI677,RFI068,RFI086. As I want to pass all values in another request. I have tried with 0,1,2,3 and -1 match no. but it returns only one value, while for -1 it returns ${ref_formCode1}. Appreciate your help. Thank you in advanced.

enter image description here

Edit:

After implementing JSR223 post-processer It shows blank field. Here are the screenshots.

enter image description here

enter image description here

enter image description here


Solution

  • You can achieve this using a JSR223 post-processer using the following code, meanwhile notice there are few syntactical errors in your JSON,

    Add the JSR223 post-processer to your request and this will do your ask

    import groovy.json.JsonSlurper;
    
    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData());
    def CodeFile = '';
    
    response.columnHeader.code.each {
      Code->if (Code == null) {}
      else {
        CodeFile += Code + ','  //this will have your code but there will be ',' at the last
      }
    }
    
    def CodeFileList = CodeFile.subSequence(0, CodeFile.length() - 1) // this will remove the last ,
    log.info('CodeFile:' + CodeFileList)
    vars.put("CodeList",CodeFileList)
    

    enter image description here

    If the code value is inside the data

    import groovy.json.JsonSlurper;
    
    def response = new groovy.json.JsonSlurper().parse(prev.getResponseData());
    def CodeFile = '';
    
    response.data.code.each {Code->
      if (Code == null) {}
      else {
        CodeFile += Code + ','  //this will have your code but there will be ',' at the last
      }
    }
    
    def CodeFileList = CodeFile.subSequence(0, CodeFile.length() - 1) // this will remove the last ,
    log.info('CodeList:' + CodeFileList)
    vars.put("CodeList",CodeFileList)
    

    Usage: Inside request body

    enter image description here

    enter image description here

    Inside the request URL

    enter image description here

    enter image description here

    In another controller,

    enter image description here

    enter image description here

    ==========

    After edit in the main question:

    enter image description here

    enter image description here