JMeter中修改提取的JSON响应并复用为PUT请求体的方法
Hey there! Let's tackle how to modify that JSON response before sending your PUT request in JMeter—here are a couple of solid, practical approaches you can use:
Approach 1: JSR223 PostProcessor (Recommended, Most Flexible)
This is the go-to method because Groovy (the preferred language for JSR223 in JMeter) handles JSON parsing/modification seamlessly, plus it’s more performant than older options like BeanShell.
- First, confirm you’ve extracted the full JSON response from your GET request into a variable (e.g.,
fullJson) using a JSON Extractor or Regular Expression Extractor. - Add a JSR223 PostProcessor directly after your GET request.
- Paste this Groovy script into the "Script" field, adjusting the property name and new value to match your needs:
import groovy.json.JsonSlurper import groovy.json.JsonBuilder // Fetch the extracted JSON string from your variable def rawJson = vars.get("fullJson") // Parse the string into a manipulable JSON object def jsonObject = new JsonSlurper().parseText(rawJson) // Modify the target property (replace "yourProperty" and "newValue" with your actual data) jsonObject.yourProperty = "newValue" // For nested properties, use dot notation: e.g., jsonObject.user.profile.email = "updated@example.com" // Convert the modified object back to a formatted JSON string def updatedJson = new JsonBuilder(jsonObject).toPrettyString() // Store the updated JSON in a new variable for your PUT request vars.put("updatedFullJson", updatedJson) - In your PUT request’s "Body Data" section, reference the new variable with
${updatedFullJson}.
Approach 2: JSON Extractor + Manual JSON Construction (For Simple Changes)
If you only need to tweak a few specific fields, this method avoids full JSON parsing:
- Use separate JSON Extractor instances to pull out all the fields you need from the GET response (both fields to keep unchanged and the one to modify). For example:
- Extract
idto variableresourceId - Extract
currentNameto variableoldName - Extract any other static fields you need
- Extract
- Add a User Defined Variables element and define your new value (e.g.,
newName=Updated Resource Name). - Add a JSR223 PreProcessor before your PUT request, then build the updated JSON manually:
def updatedJson = """ { "id": "${vars.get('resourceId')}", "name": "${vars.get('newName')}", "description": "${vars.get('resourceDescription')}" } """ vars.put("updatedFullJson", updatedJson) - Reference
${updatedFullJson}in your PUT request’s body as before.
Approach 3: JSON Transform PostProcessor (For Basic, No-Code Edits)
If you’re using JMeter 5.1+, this built-in processor works for simple field replacements:
- Add a JSON Transform PostProcessor after your GET request.
- In the "JSON Path Expression" field, enter the path to the property you want to change (e.g.,
$.yourProperty). - In the "Replacement Value" field, input your new value (e.g.,
"Updated Value"). - Set the "Destination Variable Name" to something like
updatedJson. - Use
${updatedJson}in your PUT request’s body.
Quick Tips
- Always use Groovy for JSR223 scripts (select it from the "Script Language" dropdown) — it’s faster and has better JSON support than BeanShell.
- Use a Debug Sampler + View Results Tree to verify that your modified JSON variable contains the correct data before running the PUT request.
- If your JSON has special characters (like quotes or newlines), Groovy’s
JsonBuilderwill handle automatic escaping, so you don’t have to worry about breaking the JSON structure.
内容的提问来源于stack exchange,提问作者Dominik
相关产品推荐
相关产品推荐

