简体   繁体   中英

jmeter modify json GET result and use in PUT

I'm using Jmeter to automate some tests cases. I have a JSON response from a GET, I want to change a single value of that JSON response and use the modified response in the body of a PUT. The PUT needs all the same fields as the response, and I won't know what they all are, so I don't think the JSON Path Extractor will work in my case. Efficiecy is a plus here, but I'll settle for something that just works.
I've tried this javascript in a BSF PostProcessor:

var response = SampleResult.getResponseDataAsString();
var jsonOutput = JSON.parse(response);
jsonOutput.configState = "DELETED";
vars.put("json",jsonOutput);

But the Debug Sampler shows the response as an "Object" and nothing more. Any ideas? Thanks.

I don't think you have JSON in Rhino or Nashorn, that's why your script is failing. I would suggest considering switching to JSR223 PostProcessor and use JSONBuilder and JSONSlurper like:

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper

def slurped = new JsonSlurper().parseText(SampleResult.getResponseDataAsString())
def builder = new JsonBuilder(slurped)
builder.content.configState = 'DELETED'
vars.put("json", builder.toPrettyString()) 

See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article to learn about

  • why JSR223 and Groovy is better than JavaScript
  • how to add Groovy engine support to Jmeter
  • scripting best practices

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM