4

I'm trying to use the extracted value in Body Data of another request. I extracted the value using JSON Path Extractor like below

enter image description here

value is extracted from the below response enter image description here

I used it like this in Body data

  {
    "TransactionId": ${transactionId},
    "Cancelled": false
  }

but this is not working in JMeter it shows the transactionId as ["223"]. I want it to be only 223

enter image description here

Nouman Bhatti
  • 1,341
  • 6
  • 28
  • 54

3 Answers3

8

We can do this in different ways, but I prefer to use "Jmeter" functionality (better to avoid plugins). For source git link

1. Using "Regular Expression Extractor" post processor.
2. Using "JSON Path PostProcessor" post processor.
  1. "Regular Expression Extractor": Regular Expression Extractor is no of the best functionality.

    Regular Expression: "transactionId":"(.+?)" Template: $1$ Match Number: 1

enter image description here 2."JSON Path PostProcessor": I never used this post processor, it's also good. Below is the expression

JSON path Expression : $.transactionId Match Numbers: 1

enter image description here

Please find the Results in Debugger Sampler, in "view Results tree"

enter image description here

Madhu Cheepati
  • 809
  • 5
  • 12
  • I want to write the extracted element to a file. The response JSON is : {"distance":14.518,"distanceUnit":"km","time":11.5,"timeUnit":"min"} In Json Extractor, I have given the variable name as ResponseTime. In BeanShell postprocessor, I am trying to access it as time = vars.get("ResponseTime"); But getting null. Am I doing anything wrong? please suggest – Anushree Acharjee Oct 06 '17 at 11:28
2

Substitute your $..transactionId JSON Path Expression with this one:

$..transactionId[0]

It should remove these JSON Array brackets and you will see 223 only as the ${transactionId} variable value

See Advanced Usage of the JSON Path Extractor in JMeter article for more tips on working with JSON response types.

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

In JMeter 3.1 version on-wards, try with following JSON Path expression using JSON Extractor (which is built-in)

JSON Path expression: $.transactionId
Match No: 1

gave me 265 value. with out double quotes, without array symbol.

Json extractor screenshot:

enter image description here

View results tree screenshot

enter image description here

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • in this case nothing passed in Body Data in both cases in body data it shows like TransactionId":, – Nouman Bhatti Feb 10 '17 at 06:03
  • no it doesn't form first api call response data has "transactionId": "265", with may code it show in the next api call "transactionId": ["265"], – Nouman Bhatti Feb 10 '17 at 06:17
  • could you please share the screenshot of your latest trial? Used capital `T & I`. pls check which suites the best in the expression. that might be the issue – Naveen Kumar R B Feb 10 '17 at 06:18
  • Updated answer. Add `Debug Sampler` and try to verify what is the value being captured in `transactionId` variable. you can use either `jp@gc - JSON Path Extractor` plugin OR `JSON Extractor` builtin – Naveen Kumar R B Feb 10 '17 at 06:37