MAX GRIMM
MAX GRIMM

Reputation: 119

Form a JSON request using previous response value in JMETER

On my request I am getting the following response,

{
    "studyDTO": {
        "studyId": 191,
        "studyCode": "test_ispptest2"
    },
    "sites": [],
    "subjects": [],
    "visits": [],
    "sftpLocations": [],
    "dicomLocations": [],
    "fileSystemLocations": [],
    "rawFileSystemLocations": [],
    "states": null,
    "modalities": [
        {
            "studyId": 191,
            "submitValue": "ct",
            "displayValue": "Conventional CT",
            "orderOfDisplay": 30
        },
        {
            "studyId": 191,
            "submitValue": "multi_slice_spiral_ct",
            "displayValue": "Multi-Slice Spiral CT",
            "orderOfDisplay": 50
        },
        {
            "studyId": 191,
            "submitValue": "dxa",
            "displayValue": "DXA",
            "orderOfDisplay": 60
        },
        {
            "studyId": 191,
            "submitValue": "mri",
            "displayValue": "MRI",
            "orderOfDisplay": 100
        },
        {
            "studyId": 191,
            "submitValue": "unknown",
            "displayValue": "Unknown",
            "orderOfDisplay": 240
        }
    ],
    "examDates": [],
    "series": null,
    "transferType": null,
    "customFolder": false,
    "customFile": false,
    "folderStructure": null,
    "fileStructure": null,
    "allSites": true,
    "allSubjects": true,
    "allVisits": true,
    "allStates": false,
    "allExamDates": true,
    "allModalities": false,
    "allSeries": false,
    "softEditOverride": false,
    "includePS": false,
    "includeSR": false,
    "includeRTStruct": false,
    "dicomTemplate": null,
    "errorMessage": null,
    "successMessage": null
}

in the response I received 5 modality value this can be more and in my next request body in the JSON type I want to add all the modalities, how I can do this using JSR223 Post-Processer,

Request Sample:

{
    "studyDTO": {
        "studyId": 191,
        "studyCode": "test_ispptest2"
    },
    "allVisits": true,
    "modalities": [
        {
            "studyId": 191,
            "submitValue": "ct",
            "displayValue": "Conventional CT",
            "orderOfDisplay": 30
        },
        {
            "studyId": 191,
            "submitValue": "multi_slice_spiral_ct",
            "displayValue": "Multi-Slice Spiral CT",
            "orderOfDisplay": 50
        },
        {
            "studyId": 191,
            "submitValue": "dxa",
            "displayValue": "DXA",
            "orderOfDisplay": 60
        },
        {
            "studyId": 191,
            "submitValue": "mri",
            "displayValue": "MRI",
            "orderOfDisplay": 100
        },
        {
            "studyId": 191,
            "submitValue": "unknown",
            "displayValue": "Unknown",
            "orderOfDisplay": 240
        }
    ],
    "includePS": null
}

***********************

I have developed so far, but don't have a clue to form the request JSON

import groovy.json.JsonSlurper
def jsonString = prev.getResponseDataAsString();
def jsonConvert = new JsonSlurper();
def object = jsonConvert.parseText(jsonString);
def modalityS = object.modalities.size().toString();
def modalitySize = modalityS?.isInteger() ? modalityS.toInteger() : null
for (int i = 0; i < modalitySize ; i++) {
    def modalityOrderOfDisplay = object.modalities[i].orderOfDisplay;
    def modalSubmitValue = object.modalities[i].submitValue;
    def modalDisplayValue = object.modalities[i].displayValue;
    log.info('----------------------->'+modalityOrderOfDisplay);
    log.info('----------------------->'+modalSubmitValue);
    log.info('----------------------->'+modalDisplayValue);
}

Upvotes: 0

Views: 38

Answers (1)

Dmitri T
Dmitri T

Reputation: 168157

Take a look at JsonBuilder class

Something like:

def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())

def request = [:]

request.put('studyDTO', response.studyDTO)
request.put('allVisits', response.allVisits)
request.put('modalities', response.modalities)
request.put('includePS', null)

vars.put('request', new groovy.json.JsonBuilder(request).toPrettyString())

should do the trick for you.

You will be able to refer the generated request body as ${request} where required.

More information:

Upvotes: 1

Related Questions