2

I am using a jmeter JSON extractor for a JSON that looks like this

{"type":"rpc","tid":7,"action":"SecurityManager","method":"getAuthenticationKey","result":"-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAydpVbheWPx4ZMbxJ8yCm\ndcP2EaRZD2R4PUmuFhdDdvpxT\/so00\/22orFQMgw8hrgEZ07ISzarOlclchm7DtF\nzxUzjGon1d5OJ2\/61niT+bAyuykn7y63\/BEtGS3KsR9ez3Ds+JR04Tca\/ajUYAIo\nrtAdCuvQuWkk4ZmZWywa7n899KOndL8S3G0R9Bex5XwfXJoE2BC6Ww75gwkzANFX\nIqkTYeepIMai3B8H31VIW2aJXURbjgN4yrk4sOy5a5JqnPEeCPKJR3nCrZDZGG06\ncoq0swW8oegNI9SFsiIqpDQ6Fi4WqqH5EMNu6FrkF3HAqwwyGljnogGNdnkwajiu\nCQIDAQAB\n-----END PUBLIC KEY-----\n"}

I am trying to use that value (for example just show it)

log.info("${key}")

, but I get the error

    o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script26.groovy: 8: expecting anything but ''\n''; got it anyway @ line 8, column 39.
log.info("-----BEGIN PUBLIC KEY-----

Is there something i am not doing right ?

Catalin B
  • 59
  • 7

2 Answers2

1

You should never use ${} in Groovy script in JMeter.

Instead do this:

log.info("Got key:{}", vars["key"]);

Provided your variable is named key

And this is how you would configure JSON Extractor:

JSON Extractor

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
  • This works, but can you give any insight on why ${} should not be used in groovy script and why to use vars["key"]? – Catalin B Sep 20 '18 at 08:09
0

Given you use JSR223 Test Elements already you don't need the JSON Extractor, the PEM key can be extracted and printed in one shot via JSR223 PostProcessor

  1. Add JSR223 PostProcessor as a child of the request which returns the above JSON
  2. Put the following code into "Script" area:

    vars.put('key', new groovy.json.JsonSlurper().parse(prev.getResponseData()).result)
    log.info(vars.get('key'))
    
  3. Enjoy the printed variable in Log Viewer window

    JMeter Print Variable into log

  4. Of course you will be able to access it as ${key} in the other Test Elements

References:


Going forward please avoid using JMeter functions and/or variables in Groovy scripts as they conflict with Groovy GString Templates, may be resolved into something which cause compilation or runtime failure and non-compatible with Groovy caching of compiled scripts feature negatively impacting JMeter performance.

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