简体   繁体   中英

Jmeter: JSON response manipulation and passing to the next http request

I've got the response from HTTP GET request as JSON file and I want to use that JSON and pass it to the next HTTP request. I got the following response data

{
  "apiInfo": {
      "id": "23143",
      "name": "bookkeeping",
      "state": "used",
      "data": "15893712000000"
  },
  "apiDetails": [
     {
        "bookName": "abc",
        "state": "old",
        "noOfTimesUsed": "53"
        "additionalParam"{
            "name": "abc",
            "id": "123"
        }
    },
    {
        "bookName": "def",
        "state": "new",
        "noOfTimesUsed": "5",
        "action": "keep"
        "additionalParam"{
            "name": "def",
            "id": "456"
        }
    },
    {
        "bookName": "xyz",
        "state": "avg",
        "noOfTimesUsed": "23"
        "additionalParam"{
            "name": "ghi",
            "id": "789"
        }
    },
    {
        "bookName": "pqr",
        "state": "old",
        "noOfTimesUsed": "75",
        "action": "discard"
        "additionalParam"{
            "name": "jkl",
            "id": "012"
        }
    }
  ]
}

I want to use "apiInfo" & "apiDetails" part from the JSON response and manipulate its data. As you can notice, some array field have attribute "action" in it and some one doesn't. I want to make sure all the field in the array have this data and is assigned as ' "action":"keep" '. Also, I want to add "id" from apiInfo & "name" from additionalParams from apiDetails itself. The end result I want is somewhat like this

"apiDetails": [
   {
      "id": "23143",
      "bookName": "abc",
      "state": "old",
      "noOfTimesUsed": "53",
      "action": "keep",
      "name":"abc"
   },
   {
       "id": "23143",
       "bookName": "def",
       "state": "new",
       "noOfTimesUsed": "5",
       "action": "keep",
      "name":"def"
   },
   { 
       "id": "23143",
       "bookName": "xyz",
       "state": "avg",
       "noOfTimesUsed": "23",
       "action": "keep",
      "name":"ghi"
   },
   {
       "id": "23143",
       "bookName": "pqr",
       "state": "old",
       "noOfTimesUsed": "75",
       "action": "keep",
      "name":"jkl"
    }
  ]

I've been trying to use JSR223 sampler and have been struggling with it. It's bit complicated and I need help. PS: I've tried using javascript code to manipulate the results as desired but have been unsuccessful.

Please help.

Thanks, Sid

  1. Add JSR223 PostProcessor as a child of the request which returns the above JSON
  2. Put the following code into "Script" area:

     def apiDetails = new groovy.json.JsonSlurper().parse(prev.getResponseData()).apiDetails apiDetails.each { apiDetail -> apiDetail.put('action', 'keep') } vars.put('request', new groovy.json.JsonBuilder(apidetails: apiDetails.collect()).toPrettyString())
  3. That's it, you should be able to refer the generated request as ${request} where required

More information:

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