如何解析JSR223Sampler日志响应并提取access_token到变量
access_token from Log Response & Saving as JMeter Variable (Groovy) Hey there! Let's walk through how to grab that access_token from your logged API response and store it as a reusable variable in JMeter using Groovy in the JSR223 Sampler. Here's what you need to do:
Step 1: Pull the JSON from the Log Line
Your log entry has extra prefixes (timestamp, log level, etc.), so first we'll use a regex to extract just the JSON part:
// Replace this with your actual log line, or fetch it dynamically if needed def logLine = "2018-05-31 15:38:09,291 INFO o.a.j.p.j.s.JSR223Sampler: {\"access_token\":\"acdaraffafasvtokenhgsvjdk\",\"token_type\":\"sample\",\"expires_in\":80,\"scope\":\"map.c map.r\",\"ext_attr\":{\"enhancer\":\"aqa\",\"zdn\":\"map\"},\"jti\":\"jhfeayishdgvs77sdzxcfnkjdx\"}" // Use regex to match the JSON block ({...}) def jsonMatch = logLine =~ /\{.*\}/ if (jsonMatch.find()) { def jsonString = jsonMatch.group(0) // We'll parse this JSON next } else { log.error("Oops, couldn't find JSON content in the log line!") }
Step 2: Parse JSON & Extract access_token
Groovy has a built-in JsonSlurper that makes parsing JSON a breeze. We'll use it to pull out the access_token and save it to a JMeter variable:
import groovy.json.JsonSlurper // Continuing from Step 1, inside the if block def jsonSlurper = new JsonSlurper() def responseData = jsonSlurper.parseText(jsonString) // Grab the access_token value def accessToken = responseData.access_token // Save it as a JMeter variable (you can use ${access_token} in later samplers) vars.put("access_token", accessToken) // Optional: Log it to verify it worked log.info("Successfully extracted access_token: ${accessToken}")
Full Working Code for JSR223 Sampler
Putting it all together, here's the complete script you can drop into your JSR223 Sampler:
import groovy.json.JsonSlurper // Replace with your actual log line OR use prev.getResponseDataAsString() if you're working directly with the sampler's response def logLine = "2018-05-31 15:38:09,291 INFO o.a.j.p.j.s.JSR223Sampler: {\"access_token\":\"acdaraffafasvtokenhgsvjdk\",\"token_type\":\"sample\",\"expires_in\":80,\"scope\":\"map.c map.r\",\"ext_attr\":{\"enhancer\":\"aqa\",\"zdn\":\"map\"},\"jti\":\"jhfeayishdgvs77sdzxcfnkjdx\"}" def jsonMatch = logLine =~ /\{.*\}/ if (jsonMatch.find()) { def jsonString = jsonMatch.group(0) def jsonSlurper = new JsonSlurper() def responseData = jsonSlurper.parseText(jsonString) if (responseData.access_token) { def accessToken = responseData.access_token vars.put("access_token", accessToken) log.info("Saved access_token to JMeter variable: ${accessToken}") } else { log.error("Uh-oh, no access_token found in the JSON response!") } } else { log.error("Failed to extract JSON from the provided log line.") }
Quick Tip
If you're running the HTTP call directly in the same JSR223 Sampler, skip the log parsing entirely and use prev.getResponseDataAsString() to get the raw JSON response directly:
def jsonString = prev.getResponseDataAsString()
This is more efficient and avoids any regex-related issues.
内容的提问来源于stack exchange,提问作者Hari

