Chaitra Chaitra - 4 months ago 100
JSON Question

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.

Answer

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:

Comments