Bhupender Kumawat
Bhupender Kumawat

Reputation: 41

How to extract values out of a array using JSON Extractor in jmeter?

I want to extract below json and use values accordingly.

I/p JSON:-

{
  "status": "Success",
  "message": "User created successfully",
  "id": [
    131188,
    131191
  ]
}

Here I want values of id field. I used JSON Extractor and gave expression as $.id which gives me [131188,131191] in a variable. Now I want to use individual values out of this array i.e. 131188 and 131191. Any Idea how to do it?

Update : I don't want to use 2 JSON Extractors.

Upvotes: 1

Views: 4675

Answers (2)

Dino
Dino

Reputation: 26

You could use a json extractor and a "JSR223 PostProcessor" with groovy language. An example:

   import groovy.json.JsonSlurper

   //String jsonString = vars.get("jsonFromExtractor")

   String jsonString = '''
   {
     "status": "Success",
     "message": "User created successfully",
     "id": [
       131188,
       131191
     ]
   }
   '''
   log.info("jsonString:" + jsonString)

   def json = new JsonSlurper().parseText( jsonString )

   String idValue1 = json.get("id").get(0)
   String idValue2 = json.get("id").get(1)
   log.info("idValue1:" + idValue1)
   log.info("idValue2:" + idValue2)

I hope this helps

Upvotes: 0

ararar
ararar

Reputation: 983

Just add [*] to your JSON path expression as below

$.id[*]

This will create a jmeter variable for each value.Note that you should use -1 in the match numbers field.

Upvotes: 1

Related Questions