简体   繁体   中英

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 ie 131188 and 131191. Any Idea how to do it?

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

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.

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

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