Search code examples
jsonunit-testingtestingpostmantestcase

How to extract the data from JSON file to write test case


How to extract the required parent/child node value from JSON output in postman.

I need to extract model.ConfirmPassword from the below JSON file.

{
  "Message": "The request is invalid.",
  "ModelState": {
  "model.ConfirmPassword": [
  "The password and confirmation password do not match."
  ]
}

What is the property to be passed in order to get that. jsonData.value is not working as I mentioned below.

var jsonData = JSON.parse(responseBody);
tests["Your test name"] = jsonData.value;

Image


Solution

  • The answer should be

    var jsonData = JSON.parse(responseBody);
    tests["Your test name"] = jsonData.ModelState['model.ConfirmPassword'][0] ==="The password and confirmation password do not match.";
    

    Need to pass the index as [0] in jsonData.ModelState['model.ConfirmPassword'][0] Otherwise it wont work if we want to access child elems. It can be [0] or the specific index.