简体   繁体   中英

Parsing JSON with jmeter to pass a json array as it is from previous HTTP response to a new HTTP Request

I had a query regarding fetching a part of json response obtained from 1st request in jmeter to form a new HTTP request.

I wanted to extract 1 block of info json (as it is) with doubles quotes and colon as a part of 2nd HTTP request.

  {
      "details": [
        {
          "outBound": [
            {
              "info": {
                "date": "2016-08-11",
                "class": "M",
                "code": 70,
                "pricing": [
                  {
                    "totalAmount": 68.8,
                    "totalTaxAmount": 30.8,
                    "baseFareAmount": 38.0
                  }
                ],
                "totalAmount": 68.8,
                "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "id": 1
          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 68.8,
            "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        },
        {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 78.8,
                "totalTaxAmount": 40.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "id": 2
          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "200"

            },
             { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 78.8,
            "totalDuration": 160,
            "referenceNumber": 2,
            "type": "RP",
            "duration": 160,
            "number": "100"

            }
          ]
        }
      ],
      "resultCount": {
        "count1": 1,
        "count2": 1
      },
      "displayCount": 2
    }
  ]
    }

>Expected Output: 
    {
          "info": {
            "date": "2016-08-11",
            "class": "M",
            "code": 70,
            "pricing": [
              {
                "totalAmount": 68.8,
                "totalTaxAmount": 30.8,
                "baseFareAmount": 38.0
              }
            ],
            "totalAmount": 68.8,
            "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "id": 1
          },
          "segments": [
            { 
            "date": "2016-08-11",
            "className": "Standard (W)",
            "code": 70,
            "totalAmount": 68.8,
            "totalDuration": 160,
            "referenceNumber": 1,
            "type": "RP",
            "duration": 160,
            "number": "100",

            }
          ]
        }

I tried using JSON PATH POST Processor, but I was getting the extracted data in the form of = in place of colon and string data without double quotes.

I would recommend using JSR223 PostProcessor instead of this JSON Path one.

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

     import groovy.json.JsonOutput import groovy.json.JsonSlurper def jsonSlurper = new JsonSlurper(); def response = jsonSlurper.parseText(prev.getResponseDataAsString()); def json = JsonOutput.toJson(response.details[0].outBound[0]); vars.put("json", json); 
  4. Refer extracted value as ${json} where required

References:

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