Validate array of data with json array

With my api I get an array in json form as
{
“suggestions”: [
{
“value”: “jacket dress”
},
{
“value”: “jacket”
},
{
“value”: “jacquard towel”
},
{
“value”: “jackets”
},
{
“value”: “jacquard”
}
]
}

I need to validate each value of “value:” with some expcted array of data. Being Postman newborn , how can this be achieved. Where can i define my actual data? and how? .

can i have this in pre request script, let jac=[{value:“jacket dress”},{value:“jacket”},{value:“jacquard towel”},{value:“jacquard”},{value:“jackets”}];. Im lost pls help.

Hi @Jaggs_jaggy75 :wave:

So here we need two arrays. One is user defined, where you are planning to define the expected. And another array where the results from the response is stored in it. Finally we need to verify each other.

Please find below the snippets.

Under pre-request script you can define the array with expected values:

var result = ["jacket dress","jacket","jacquard towel","jackets","jacquard"];

pm.environment.set("result", result);

Under tests section,

    var resp = pm.response.json();
    console.log(resp);

    respArray = [];

    for (var i=0; i<resp.suggestions.length; i++)
    {
respArray.push(`${resp.suggestions[i].value}`);
   //console.log(respArray);
pm.environment.set('respArray', (respArray));
    }
    console.log(respArray);
console.log(pm.environment.get("result"));


pm.test("Validate the array", function () {
    var _ = require('lodash');
    _.isEqual(respArray, pm.environment.get("result"));
});

Please let me know if works :slight_smile:

Thanks @bpricilla , it works :smiley:. I had simply copy-pasted your code, now i need to understand it.

1 Like

Wow, Glad to hear it :slight_smile:

Is there a way to make it work with more data set. I want to have key-value data like Json.

for e.g I input a value and depending upon that I look for its value in defined data. Hope Im not confusing you or is it a realistic case.

Its almost the same, infact simple. Under pre-req script:

let jac = [
   {
      "value":"jacket dress"
   },
   {
      "value":"jacket"
   },
   {
      "value":"jacquard towel"
   },
   {
      "value":"jacquard"
   },
   {
      "value":"jackets"
   }
];

pm.environment.set("result", jac);

Under Tests:

    var resp = pm.response.json();
    console.log(resp.suggestions);

   respArray= resp.suggestions;
   
console.log(pm.environment.get("result"));


pm.test("Validate the array", function () {
    var _ = require('lodash');
    _.isEqual(respArray, pm.environment.get("result"));
});

Please let me know if this helps :slight_smile:

Sorry but I was too fast in replying now I changed my input param and still see that test has passed.
The response body {
“suggestions”: [{
“value”: “jean’s”
},
{
“value”: “jean’s ladies”
},
{
“value”: “jeans men”
},
{
“value”: “jeans kids”
},
{
“value”: “jeans high waist”
}
]
}
Comparing with result variable, It should fail :roll_eyes:

Oh okay, even I didn’t check the response subset properly. Well in that case, I believe you need to iterate through the expected result and store the values in an array and then you need to validate against the actual result. So I believe we can follow the first approach to make it simple.

I was referring to first solution you provided. That itself does not check response array.

Oh I get that, can you try the below snippet please

pm.test("Validate the array", function () {
    if (JSON.stringify(respArray) === JSON.stringify(pm.environment.get("result"))){
    console.log('Both arrays are equal!!');
} else {
    pm.expect.fail('This failed because Arrays are not equal');
    console.log('Arrays are not equal');
}
    });

Now it works with +ve and -ve data, thanks again

1 Like

@bpricilla How will a validate of my whole JSON response array of objects have more than 350 objects.


I want to check the whole response body has a parameter with datatype.
Ya could you tell me what will be the best practice for this.
currently, I m converting JSON response from the swagger doc and in postman that response body i m storing in a variable like this…
var schema=
{
“type”: “array”,
“items”: [
{
“type”: “object”,
“properties”: {
“modifiedOn”: {
“type”: “string”
},
“id”: {
“type”: “string”
},
“header”: {
“type”: “string”
},
“content”: {
“type”: “string”
},
“wordsCount”: {
“type”: “integer”
},

  },
  "required": [
    "modifiedOn",
    "id",
    "header",
    "content",
    "wordsCount",
  ]
}

]
};
pm.test(“Validate response body”, function() {
pm.response.to.have.jsonSchema(schema);
});