# How to write a validation script for values contained in arrays

**URL:** <https://community.postman.com/t/how-to-write-a-validation-script-for-values-contained-in-arrays/5174>\
**Category:** Help Hub\
**Tags:** tests\
**Created:** [April 26, 2019, 2:49pm UTC](https://community.postman.com/t/how-to-write-a-validation-script-for-values-contained-in-arrays/5174 "2019-04-26T14:49:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![debjchak](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@debjchak](https://community.postman.com/u/debjchak)\
**Post date:** [April 26, 2019, 2:49pm UTC](https://community.postman.com/t/how-to-write-a-validation-script-for-values-contained-in-arrays/5174/1 "2019-04-26T14:49:52Z")

</div>

I have a response body and I want to check if the Errors received in the body has `"rejectionErrorCode": "YYY"`.

Please find my response body below:

```
{
    "acknowledgement": {
        "messageHeader": {
            "testFlag": 0,
            "sourceMessageID": "TEST"
        },
        "messageType": "1",
        "status": "299",
        "primaryRepositoryID": "XXX",
        "Errors": [
            {
                "rejectionErrorCode": "YYY",
                "rejectionErrorDescription": "ZZZ",
                "rejectionErrorData": "AAA"
            }
        ]
    }
}
```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [April 26, 2019, 3:16pm UTC](https://community.postman.com/t/how-to-write-a-validation-script-for-values-contained-in-arrays/5174/2 "2019-04-26T15:16:44Z")

</div>

Hey @debjchak

Welcome to the community 👋

This could be used as a test to check for that value:

```
pm.test("Response Value Check", () => {
    let jsonData = pm.response.json()
    pm.expect(jsonData.acknowledgement.Errors[0].rejectionErrorCode).to.equal("YYY");
});   

```

The main part that’s doing all the work to drill down through the response, to get to the value is `jsonData.acknowledgement.Errors[0].rejectionErrorCode`. Think of it as just stepping down through the JSON. For each of the `keys`, you’re taking a single step down.

The one important thing is that `Errors` is an `array` or a list of `objects` and you need to specify which item from the list you want.

In this case, it’s the `first` item so we reference this with a `[0]` as this is zero indexed.

We can then drop down a step into the first item, where we find the `rejectionErrorCode` property whose value we want to check.

We can assert against that property using the `pm.expect()` function.

`pm.expect(jsonData.acknowledgement.Errors[0].rejectionErrorCode).to.equal("YYY")`

If the `Errors` array had more than one object and you wanted to check each one, you will need to iterate through them.

This is one way that you could do that:

```
pm.test("Response Value Check", () => {
    let jsonData = pm.response.json().acknowledgement.Errors
    _.each(jsonData, (arrItem) => {
        pm.expect(arrItem.rejectionErrorCode).to.equal("YYY");
    })
});

```

Hope this all makes sense and helps you out. 🙂

---

<div class="post-metadata">

**Author:** ![ofir1212](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@ofir1212](https://community.postman.com/u/ofir1212)\
**Post date:** [June 2, 2021, 5:16am UTC](https://community.postman.com/t/how-to-write-a-validation-script-for-values-contained-in-arrays/5174/3 "2021-06-02T05:16:38Z")

</div>

Hi @danny-dainton  
And what if I have an array, and I would like the test to pass if ANY OF rejectionErrorCode values in the array equals the expected value?  
for example, the test will pass if the expected value is BBB and the response body is this:

{  
“acknowledgement”: {  
“messageHeader”: {  
“testFlag”: 0,  
“sourceMessageID”: “TEST”  
},  
“messageType”: “1”,  
“status”: “299”,  
“primaryRepositoryID”: “XXX”,  
“Errors”: [  
{  
“rejectionErrorCode”: “YYY”,  
“rejectionErrorDescription”: “ZZZ”,  
“rejectionErrorData”: “AAA”  
},  
{  
“rejectionErrorCode”: “BBB”,  
“rejectionErrorDescription”: “ZZZ”,  
“rejectionErrorData”: “AAA”  
}  
]  
}  
}
