2

Calling a REST API with JMeter 3.3, I have following JSON response:

{"map":{},"meta":{"code":"123"}} 

How can I extract the value of code (123)?

So far I am using this:

  • Thread Group
  • HTTP Request
  • JSON Extractor

with this 2 vars: code; meta and this json path expressions: $.code; $.meta

  • JSR223 Assertion

with this Groovy code:

String codeString = vars.get("code"); String meta = vars.get("meta"); log.info ("The code answer is " + codeString); if (codeString != "000"){ AssertionResult.setFailureMessage("The code is: " + codeString + " - meta is: " + meta); AssertionResult.setFailure(true); } 

this is the assertion result instead:

Assertion error: false Assertion failure: true Assertion failure message: The code is: No_Default - meta is: {"code":"000"} 

3 Answers 3

3

You can use JsonSlurper to extract the data you are interested in:

import groovy.json.JsonSlurper String json = prev.getResponseDataAsString() def root = new JsonSlurper().parseText(json) def code = root.meta.code 
Sign up to request clarification or add additional context in comments.

Comments

3

Given you use Groovy you don't need the JSON Path Extractor, you can validate your code like:

def code = com.jayway.jsonpath.JsonPath.read(prev.getResponseDataAsString(), '$..code').get(0).toString() if (!code.equals('000')) { AssertionResult.setFailure(true) AssertionResult.setFailureMessage('The code is ' + code) } 

More information:

Comments

2

You have a mistake in JSON path expression $.code for getting code, it's under second hierarchy and therefore you are missing ., use the following:

$..code 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.