Search code examples
jsongroovyjmeter

Append a value of a JSON inside another JSON in JMeter


By using the following script, I am able to generate the request payload for a request method.

import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
def response = new groovy.json.JsonSlurper().parse(prev.getResponseData())
def builder = new JsonBuilder()
def finalRequest = [:];
def dicomTemp = builder.dicomTemplate
    {
    templateName "Default"
    templateDesc "Default"
    templateType "DEFAULT"
    }
def dicomTags = builder.dicomTaS {
    {
    tagGroup "0002"
    tagElement "0002"
    tagName "Media Storage SOP Class UID"
    updatedOn "2021-10-05T22:03:36.000+00:00"
    labelValue 131074
    mandatory "1"
    }
    {
    tagGroup "0002"
    tagElement "0010"
    tagName "Transfer Syntax UID"
    tagKeyword "TransferSyntaxUID"
    createdOn "2021-06-02T20:40:59.000+00:00"
    numericLabel "00020010"
    labelValue 131088
    mandatory "1"
    }
}
finalRequest.put('studyDTO', true);
finalRequest.put('allSites', true);
finalRequest.put('allSubjects', true);
finalRequest.put('allStates', true);
finalRequest.put('allVisits', true);
finalRequest.put('modalities', response.modalities);
finalRequest.put('allModalities', true);
finalRequest.put('allExamDates', true);
finalRequest.put('allSeries', true);
finalRequest.put('transferType', "DICOM");
finalRequest.put('sftpLocations', response.sftpLocations)
finalRequest.put('dicomLocations', response.dicomLocations)
finalRequest.put('customFolder', null)
finalRequest.put('folderStructure', null)
finalRequest.put('customFile', null)
finalRequest.put('fileStructure', null)
finalRequest.put('includePS', null)
finalRequest.put('softEditOverride', true)
finalRequest.put('dicomTemplate', dicomTemp.dicomTemplate)
finalRequest.put('dicomTemplate.dicomTags', [dicomTags.dicomTaS])
vars.put('finalPayload',new groovy.json.JsonBuilder(finalRequest).toPrettyString());

But there are two corrections require here,

enter image description here

The dicomTags should be a key of dicomTemplate, but it is placed separately because of the declaration dicomTemplate.dicomTags. How to rectify this?

enter image description here

Can see the second value, want to include all the values

enter image description here

Like below

enter image description here


Solution

  • I think you need to change this:

    def dicomTags = builder.dicomTaS {
    {
      tagGroup "0002"
      tagElement "0002"
      tagName "Media Storage SOP Class UID"
      updatedOn "2021-10-05T22:03:36.000+00:00"
      labelValue 131074
      mandatory "1"
    }
    {
      tagGroup "0002"
      tagElement "0010"
      tagName "Transfer Syntax UID"
      tagKeyword "TransferSyntaxUID"
      createdOn "2021-06-02T20:40:59.000+00:00"
      numericLabel "00020010"
      labelValue 131088
      mandatory "1"
    }
    }
    

    to this:

    def dicomTags = builder {
        dicomTaS(
                [
                        {
                            tagGroup "0002"
                            tagElement "0002"
                            tagName "Media Storage SOP Class UID"
                            updatedOn "2021-10-05T22:03:36.000+00:00"
                            labelValue 131074
                            mandatory "1"
                        },
                        {
                            tagGroup "0002"
                            tagElement "0010"
                            tagName "Transfer Syntax UID"
                            tagKeyword "TransferSyntaxUID"
                            createdOn "2021-06-02T20:40:59.000+00:00"
                            numericLabel "00020010"
                            labelValue 131088
                            mandatory "1"
                        }
                ]
        )
    }
    

    and this:

    finalRequest.put('dicomTemplate', dicomTemp.dicomTemplate)
    finalRequest.put('dicomTemplate.dicomTags', [dicomTags.dicomTaS])
    

    to this:

    dicomTemp.dicomTemplate.put('dicomTags', dicomTags['dicomTaS'])
    finalRequest.put('dicomTemplate', dicomTemp.dicomTemplate)
    

    also don't post code as screenshots and include everything we need to reproduce your issue (i.e. response object): How do I ask a good question?

    More information: