如何用JMeter的JSON Path Extractor提取href中的随机值字符串?
Hey there! Let's tackle your two JMeter questions step by step—these are common tasks once you get the hang of JMeter's post processors.
Question 1: General Way to Grab a Random Value from an href via JSON Path Extractor
First, a quick note: if your href is embedded as a string in a JSON response (like "href": "/some/path/RANDOM_STRING?foo=bar"), you'll usually need two steps: extract the full href string with JSON Path, then use a regex to pull out the random part. Here's how:
Step 1: Add a JSON Path Extractor to your HTTP Request
- Right-click your request → Post Processors → JSON Path Extractor
- Set Reference Name to something descriptive, like
fullHref - Enter the JSON Path Expression that points to your href field. For example, if your JSON looks like
{"navLink": {"href": "/auth/token/xyz123?expire=123"}}, use$.navLink.href - Leave Match No. set to
1(since you probably want the first match)
Step 2: Add a Regular Expression Extractor to isolate the random value
- Right-click the same request → Post Processors → Regular Expression Extractor
- Set Reference Name to
randomHrefValue - Write a regex that matches your random value's position. If the random string comes after
/token/and before?, use\/token\/(.+?)\? - Set Template to
$1$(this captures the first group from the regex) - Match No. stays at
1
If the random value already exists as a standalone field in the JSON (like the sessionName in your second question), you can skip the regex step and just use JSON Path directly—way simpler!
Question 2: Extract "eL7tYgxhYh" from Your Specific JSON Response
Your given JSON is:
{ "sessionName":"eL7tYgxhYh", "imageSrc":"/Common/CaptchaImage/eL7tYgxhYh?t=636573960197174426" }
You have two straightforward options here:
Method 1: Pull Directly from sessionName (Best & Easiest!)
Since the random value is already a top-level field, just use JSON Path:
- Add a JSON Path Extractor to your request
- Reference Name:
sessionValue - JSON Path Expression:
$.sessionName - Done! You can now use
${sessionValue}in any subsequent requests to reference this value.
- Reference Name:
Method 2: Extract from the imageSrc URL
If for some reason you can't use sessionName, you can extract it from the imageSrc string instead:
- First get the full
imageSrcwith JSON Path:- JSON Path Extractor settings:
- Reference Name:
fullImageSrc - JSON Path Expression:
$.imageSrc
- Reference Name:
- JSON Path Extractor settings:
- Then use a regex to pull out the random part:
- Add a Regular Expression Extractor
- Reference Name:
randomImageValue - Regular Expression:
\/CaptchaImage\/(.+?)\? - Template:
$1$ - Match No.:
1
Either method will give you the exact "eL7tYgxhYh" value you need.
内容的提问来源于stack exchange,提问作者Anthony Phan

