# Validate array of data with json array

**URL:** <https://community.postman.com/t/validate-array-of-data-with-json-array/19879>\
**Category:** Help Hub\
**Created:** [February 2, 2021, 5:59pm UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879 "2021-02-02T17:59:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 2, 2021, 5:59pm UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/1 "2021-02-02T17:59:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 12:20am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/2 "2021-02-03T00:20:32Z")

</div>

Hi @Jaggs_jaggy75 👋

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:

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

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

```

Under tests section,

```auto
    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 🙂

---

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 3, 2021, 8:48am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/3 "2021-02-03T08:48:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 8:55am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/4 "2021-02-03T08:55:39Z")

</div>

Wow, Glad to hear it 🙂

---

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 3, 2021, 8:58am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/5 "2021-02-03T08:58:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 9:11am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/6 "2021-02-03T09:11:15Z")

</div>

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

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

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

```

Under Tests:

```auto
    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 🙂

---

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 3, 2021, 9:15am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/7 "2021-02-03T09:15:53Z")

</div>

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 🙄

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 9:35am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/8 "2021-02-03T09:35:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 3, 2021, 9:49am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/9 "2021-02-03T09:49:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bpricilla](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/bpricilla/32/6511_2.png) [@bpricilla](https://community.postman.com/u/bpricilla)\
**Post date:** [February 3, 2021, 11:02am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/10 "2021-02-03T11:02:31Z")

</div>

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

```auto
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');
}
    });

```

---

<div class="post-metadata">

**Author:** ![Jaggs\_jaggy75](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/jaggs_jaggy75/32/7044_2.png) [@Jaggs\_jaggy75](https://community.postman.com/u/Jaggs_jaggy75)\
**Post date:** [February 3, 2021, 11:50am UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/11 "2021-02-03T11:50:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![atifkhan5425](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/atifkhan5425/32/29621_2.png) [@atifkhan5425](https://community.postman.com/u/atifkhan5425)\
**Post date:** [July 9, 2022, 9:52pm UTC](https://community.postman.com/t/validate-array-of-data-with-json-array/19879/12 "2022-07-09T21:52:30Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/d/ddaa1460f1bae877b25b1659d55cf91ec1e8708d.jpeg)  
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);  
});
