0

In Jmeter when trying to extract data from Json object everything is ok and works great but, when i have array of Json objects can't extract it.

Result is:

[{"id":1,"name":"test"},{"id":2,"name":"test2"}]

it is my project JSON Extractor and JSR233 Assertion.enter image description here

enter image description here

Inside Groovy script i'm making log.info but it doesn't captures variable value which is described inside JSON Extraxtor.

String id =  vars.get("id");
 log.info ("The example answer is " + id);
   if (id == ""){
        AssertionResult.setFailureMessage("The id is  null");
            AssertionResult.setFailure(true); 
    } 

Please note that if Json response looks like this {"id":1,"name":"test"} everything works correctly.

1 Answer 1

1
  1. Change your JSON Path Expression to look like: $..id. .. is a deep scan operator so it will return all id attributes values.
  2. Change Match No to -1

It will result in the following variables:

id_1=1
id_2=2
id_matchNr=2

I have no idea what exactly you need to assert, hopefully you will be able to amend your Groovy script yourself.

Also be aware that there is JSON Assertion test element available since JMeter 4.0 so you won't need to have separate extractor and assertion elements.

Sign up to request clarification or add additional context in comments.

1 Comment

i want to check if any property inside Json is null or empty. just added $..id and -1 now response changed. The example answer is null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.