Search code examples
arraysjsonsoapuijsonpath

Retrieve information in JSONPath SoapUI


I have a JSON response in SoapUI that looks like this:

{
  "civilite" : "1" ,
  "nom" : "Fitz",
  "prenom" : "Quinn",
  "dateN" : "07/10/1953"
}

But I want to use JsonPath to retrieve only a part of this data, so I could have something like this:

{
  "nom" : "Fitz",
  "prenom" : "Quinn"
}

Is there a way to apply a JsonPath expression to retrieve this information?


Solution

  • For the first request step, add Script Assertion and use below script. The script extract the nom and prenom values and set them at test case level custom properties with given property names. With Script Assertion, an additional Groovy Script test step can be avoided.

    Then in the next test step, use property expansion, so that those values will be replaced automatically with actual values by soapui.

    Script Assertion:

    //Check if the response is non empty or null
    assert context.response
    
    //Parse Json
    def parsedJson = new groovy.json.JsonSlurper().parseText(context.response)
    log.info "Nom: ${​parsedJson.nom}"
    log.info "Prenom: ${parsedJson.prenom​}"
    
    //assert if nom and prenom are not empty
    assert parsedJson.nom, "nom is null or empty in the response"
    assert parsedJson.prenom, "prenom is null or empty in the response"
    
    //Set the retrieved values at test case level properties NOM, PRENOM
    context.testCase.setPropertyValue('NOM', parsedJson.nom as String)
    context.testCase.setPropertyValue('PRENOM', parsedJson.prenom as String)
    

    Change 2nd step request content as below with Property Expansion

    {
      "nom" : "${#TestCase#NOM}",
      "prenom" : "${#TestCase#PRENOM}"
    }