Home > Back-end >  How to check postman response array value when attribute name more than one word
How to check postman response array value when attribute name more than one word

Time:12-15

I have the following response to my postman request. I want to check the "Duplicate Subscription" array first item value contains the expected text.

Can you help me with the text, please?

My Response:

{
  "type": "https://httpstatuses.com/409",
    "title": "Conflict",
      "status": 409,
        "detail": "A resource with the specified parameters already exist",
          "correlationId": "f976f20f-c9b1-4822-aa15-2b5e5de3555a",
            "errors": {
    "Duplicated Subscription": [
      "Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing."
    ]
  }
}

This is my test which failed line number 7 :

pm.test("Validate conflict Subscription stauts code", function () {
  pm.response.to.have.status(409);
  var jsonData = pm.response.json();
  pm.expect(jsonData).to.have.property("title");
  pm.expect(jsonData).to.have.property("detail");
  pm.expect(jsonData.errors).to.have.property("Duplicated Subscription");
  pm.expect(jsonData.errors).to.have.property("Duplicated Subscription")[0].to.contains("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");
});

CodePudding user response:

use this test unit:

I don't know what kind of unit test you are using so please read the comment and translate it into syntax your unit test supports

pm.test("Validate conflict Subscription stauts code", function(){
  pm.response.to.have.status(409);
  var jsonData = pm.response.json();
  pm.expect(jsonData).to.have.property("title");
  pm.expect(jsonData).to.have.property("detail");
  
  //pm.expect(jsonData.errors).to.have.property("Duplicated Subscription");
  //pm.expect(jsonData.errors).to.have.property("Duplicated Subscription")[0].to.contains("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");

  // i don't know what unit test runner you use so i will use the unit test syntax i am familiar with and explain to you
  pm.expect(jsonData.errors["Duplicated Subscription"]).not.toBeUndefined() // check if this exists . if yes it will !== undefined
  pm.expect(jsonData.errors["Duplicated Subscription"][0]).toEqual("Active subscriptions has reached the limit of 6 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing."); // check this is equal ===
});

CodePudding user response:

Found the answer to this.

pm.expect(jsonData.errors).to.have.property("Duplicated Subscription").contains("Active subscriptions has reached the limit of 5 for CarrierId=AB, CustomerId=xyz. Cancel existing subscription before subscribing.");

CodePudding user response:

When the attribute name of an item in a JSON array has multiple words, you can access it using bracket notation in Postman.

For example, suppose you have a JSON array with an attribute called first name

[  {    "first name": "John",    "last name": "Doe"  },  {    "first name": "Jane",    "last name": "Smith"  }]

To access the first name attribute of the first item in the array, you can use the following code in the Postman test script:

pm.test("Check first name", function() {
  // Get the response body as a JSON object
  var jsonBody = pm.response.json();

  // Access the first item in the array
  var firstItem = jsonBody[0];

  // Access the "first name" attribute of the first item using bracket notation
  var firstName = firstItem["first name"];

  // Assert that the "first name" attribute has the expected value
  pm.expect(firstName).to.equal("John");
});

In the code above, we use bracket notation (firstItem["first name"]) to access the first name attribute of the first item in the array. We then use the Postman expect() method to assert that the value of the attribute is equal to the expected value.

You can also use dot notation to access the first name attribute, but you need to enclose the attribute name in quotes to make it a valid identifier in JavaScript:

var firstName = firstItem["first name"];
// This is equivalent to:
var firstName = firstItem.first name; // Invalid syntax!
var firstName = firstItem['first name']; // Valid syntax

Using bracket notation is generally more versatile and allows you to use any string value as the attribute name, even if it contains spaces or other special characters.

  • Related