0

I'm trying to use Postman as a test tool to validate that our customers all have a mailing address in our master system. I'm having trouble drilling down into the JSON due to its structure. Each response is an array structure with a single "node" that has no "head attribute" to address.

Example JSON:


[
  {
    "ID": "cmd_org_628733899",
    "organization": {
      "name": "FULL POTENTIAL",
      "accountStatusCode": "1",
      "accountStatusDescription": "OPEN"
    },
    "location": [
      {
        "locality": "LITTLE ROCK",
        "locationType": "MAILING"
      },
      {
        "locality": "BIG ROCK",
        "locationType": "LOCATION"
      }
    ]
  }
]

Test code as it exists:

pm.test("Check for a Mailing Address", function () {
   // Parse response body
   var jsonData = pm.response.json();

   // Find the array index for the MAILING Address
   var mailingLocationIndex = jsonData.location.map(
          function(filter) {
             return location.locationType; 
          }
    ).indexOf('MAILING'); 

   // Get the mailing location object by using the index calculated above
   var mailingLocation = jsonData.location[mailingFilterIndex];

   // Check that the mailing location exists
   pm.expect(mailingLocation).to.exist;

});

Error message: TypeError: Cannot read property 'map' of undefined

I understand that I have to iterate to node(0) in the outer array and then drill into the nested location array to find an entry with a locationType = Mailing.

I can't get past the outer array. I'm new to JavaScript and JSON parsing - I am a COBOL programmer.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Do you thing you could indent your code properly? – Tomalak Jun 26 '20 at 20:07
  • Sure - it lost it's formatting along the way. Apologies. – James Mattheiss Jun 26 '20 at 20:18
  • 2
    the brackets still do not match in the json example – Palo Jun 26 '20 at 20:19
  • Yes - just corrected that - the markup didn't like the [ on the same line as the ``` indicating code block. – James Mattheiss Jun 26 '20 at 20:20
  • please try again :) – Palo Jun 26 '20 at 20:21
  • So you have an array of items that *each* can have a mailing address. Your code is only for a single item that can have a mailing address (and possibly others types of address). What's your goal? Filter out all the items that have a mailing address? – Tomalak Jun 26 '20 at 20:22
  • The json will contain a single customer's information with an array of "location" data. I want identify if ANY of the entries in the "location" array have a locationType = "MAILING". This is complicated from my perspective as the entire json is an array. I don't know how to address the 0 node. I'm 99% sure my code would work if the json response wasn't an array. – James Mattheiss Jun 26 '20 at 20:32
  • That's a contradiction. You can't have both a "a single customer's information" and "the entire json is an array". Which one is it? – Tomalak Jun 26 '20 at 20:33
  • Please look at the revised corrected JSON. The customer information is returned as an array with a single entry. – James Mattheiss Jun 26 '20 at 20:36
  • why not to use just jsonData[0] ? – Palo Jun 26 '20 at 20:37
  • 1
    Thank you for your help. That was the part I was missing - how to address/extract index(0) - so simple. – James Mattheiss Jun 26 '20 at 21:13

1 Answers1

0

Knowing nothing else, I would say you mean this

pm.test("Check for a Mailing Address", function () {
    var mailingLocations = pm.response.json().location.filter(function (item) {
        return item.locationType === 'MAILING';
    });
    pm.expect(mailingLocations).to.have.lengthOf(1);
});

You want to filter out all the locations that have a MAILING type and there should be exactly one, or at least one, depending.

Whether pm.response.json() actually returns the object you show in your question is impossible to say from where I'm standing.


In modern JS, the above is shorter:

pm.test("Check for a Mailing Address", function () {
    var mailingLocations = pm.response.json().location.filter(item => item.locationType === 'MAILING');
    pm.expect(mailingLocations).to.have.lengthOf(1);
});
Tomalak
  • 332,285
  • 67
  • 532
  • 628