Search code examples
parametersjmeterextractbeanshell

JMeter BeanShell PostProcessor to extract date until T (time zone)


I am extracting Date from json in the following format: 1980-09-08T00:00:00Z. To reuse this I need only: 1980-09-08. So I trie dto use BeanShell postprocessor:

String varPurchaseDate = ${PurchaseDate};
log.info(varPurchaseDate);
String[] varDate = line.split("T");
log.info(varDate[0]);

I am getting error

2017/11/01 16:41:30 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   In file: inline evaluation of: ``String varPurchaseDate = 1980-09-08T00:00:00Z; log.info(varPurchaseDate);'' Encountered "9" at line 1, column 32.

Please help. Also how will I be able to use the parameter as input, as varDate[0]?


Solution

    1. Don't reference JMeter Variables or Functions in script body, use "Parameters" section or code-based equivalents instead
    2. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting

    So

    • Switch to JSR223 Post Processor
    • Tick Cache compiled script if available box
    • Replace first line of your code with:

      String varPurchaseDate = vars['PurchaseDate']
      

    One more recommendation is using JMeter built-in components where possible, particularly in your case you can use __split() function to get first part of your date like:

    ${__split(${PurchaseDate},date,T)}
    

    It will generate the following variables:

    date_1=1980-09-08
    date_2=00:00:00Z 
    date_n=2
    

    So you will be able to refer the desired value as ${date_1} where required

    JMeter Split Function Demo