Check first value from nested array then stop checking

Hello! I’m trying to check the value entry from the first array entry under history>field_2 from this response (scroll to history section at end of the response).

{
    "formMetadataId": "65f84db93192455ba7206094",
    "permissions": {
        "canView": true,
        "canEdit": true,
        "canFinalize": false,
        "canReview": false,
        "canReview2": false,
        "canQuery": false,
        "canSign": false,
        "canEditFinalState": false
    },
    "sections": [
        {
            "name": "Study_Info",
            "label": "Study Info",
            "hasFixedIndex": false,
            "indexed": false,
            "rows": [
                {
                    "label": "",
                    "table": {
                        "numRows": 1,
                        "numCols": 1,
                        "width": "100%",
                        "alignment": "center",
                        "autoNumber": true,
                        "showBorders": true,
                        "columnHeaders": [],
                        "columnWidths": [
                            "",
                            ""
                        ],
                        "columnHeadersSortBy": [],
                        "rowLabels": [],
                        "tableRows": [
                            {
                                "cells": [
                                    {
                                        "align": "center",
                                        "layoutOrientation": "vertical",
                                        "components": [
                                            {
                                                "fieldType": "text",
                                                "fieldName": "field_1",
                                                "fieldLabel": "Study Name",
                                                "usingDimensions": true,
                                                "usingWidth": false,
                                                "labelClass": "labelTop",
                                                "labelColor": "#000000",
                                                "readOnly": false,
                                                "requireChangeReason": true,
                                                "changesState": true,
                                                "showChanges": true,
                                                "addInfoIcon": false,
                                                "showBackground": true,
                                                "fieldHasInitialValue": false,
                                                "canEditMerged": "APS1I",
                                                "canViewMerged": "ACDIMPSTZ123",
                                                "imgWidth": 200,
                                                "imgHeight": 200,
                                                "_id": "form_element_0",
                                                "fieldId": "661956e2a5d06569f92df079",
                                                "value": "Test Value Entry Change",
                                                "changeReasonStatus": "Not Required",
                                                "comments": [],
                                                "queries": []
                                            },
                                            {
                                                "fieldType": "text",
                                                "fieldName": "field_2",
                                                "fieldLabel": "Description",
                                                "usingDimensions": true,
                                                "usingWidth": false,
                                                "labelClass": "labelTop",
                                                "labelColor": "#000000",
                                                "readOnly": false,
                                                "requireChangeReason": true,
                                                "changesState": true,
                                                "showChanges": true,
                                                "addInfoIcon": false,
                                                "showBackground": true,
                                                "fieldHasInitialValue": false,
                                                "canEditMerged": "APS1I",
                                                "canViewMerged": "ACDIMPSTZ123",
                                                "imgWidth": 200,
                                                "imgHeight": 200,
                                                "_id": "form_element_1",
                                                "fieldId": "661956e2a5d06569f92df082",
                                                "value": "Test Value Entry2 Change",
                                                "changeReasonStatus": "Not Required",
                                                "comments": [],
                                                "queries": []
                                            }
                                        ]
                                    }
                                ]
                            }
                        ]
                    }
                }
            ],
            "sectionErrors": []
        }
    ],
    "hasErrors": false,
    "hasWarning": false,
    "stateList": [
        {
            "firstName": "R.",
            "lastName": "Tieken",
            "userId": "65c52ccbba91e986a9f43041",
            "userProfileId": "64e4e6e1dc3b0653e7058e42",
            "role": {
                "code": "A",
                "name": "Administrator"
            },
            "when": 1712936674327,
            "value": "form.state.in.work",
            "_id": "661956e2a5d06569f92df08a"
        }
    ],
    "lockStateList": [],
    //HERE IS WHERE I AM ASKING ABOUT!//
"history": {
        "field_2": [
            {
                "fieldId": "661956e2a5d06569f92df082",
                "id": "66195ca5a5d06569f92df496",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "I",
                    "name": "Investigator"
                },
                "when": 1712938149562,
                "value": "Test Value Entry2 Change",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df082",
                "id": "66195c6ba5d06569f92df457",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712938091489,
                "value": "Test Value Entry2",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df082",
                "id": "661956f8a5d06569f92df122",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "I",
                    "name": "Investigator"
                },
                "when": 1712936696910,
                "value": "Test Value Entry2 Change",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df082",
                "id": "661956e5a5d06569f92df0a3",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712936677160,
                "value": "Test Value",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df082",
                "id": "661956e2a5d06569f92df086",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712936674310,
                "value": "Test Value Entry2",
                "changeReason": {
                    "firstName": "system",
                    "lastName": "",
                    "role": {
                        "code": "$",
                        "name": "System"
                    },
                    "when": 1712936674315,
                    "value": "initial value"
                },
                "sectionName": "Study_Info"
            }
        ],
        "field_1": [
            {
                "fieldId": "661956e2a5d06569f92df079",
                "id": "66195ca5a5d06569f92df48f",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "I",
                    "name": "Investigator"
                },
                "when": 1712938149527,
                "value": "Test Value Entry Change",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df079",
                "id": "66195c6ba5d06569f92df450",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712938091444,
                "value": "Test Value Entry",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df079",
                "id": "661956f8a5d06569f92df11b",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "I",
                    "name": "Investigator"
                },
                "when": 1712936696875,
                "value": "Test Value Entry Change",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df079",
                "id": "661956e5a5d06569f92df09c",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712936677128,
                "value": "Test Value",
                "sectionName": "Study_Info"
            },
            {
                "fieldId": "661956e2a5d06569f92df079",
                "id": "661956e2a5d06569f92df07d",
                "firstName": "R.",
                "lastName": "Tieken",
                "role": {
                    "code": "A",
                    "name": "Administrator"
                },
                "when": 1712936674265,
                "value": "Test Value Entry",
                "changeReason": {
                    "firstName": "system",
                    "lastName": "",
                    "role": {
                        "code": "$",
                        "name": "System"
                    },
                    "when": 1712936674278,
                    "value": "initial value"
                },
                "sectionName": "Study_Info"
            }
        ]
    },
    "downloadHistory": {}
}

If I run this, the test passes the first value, but then continues to check the next set in the array, which I do not want to check.

pm.test('The expected latest value for field_2 is returned in history.', function () {
    pm.expect(pm.response.json().history).to.be.an('object');
    Object.values(pm.response.json().history).forEach(history => {
        pm.expect(history).to.be.an('array');
        history.forEach(field => {
            pm.expect(field).to.have.property('fieldId');
            var field2 = (pm.collectionVariables.get('field2Id'));
            if (field.fieldId === field2) {
                pm.expect(field.value).to.equal('Test Value Entry2 Change');
            }
        });
    });
});

Test Results:
FAIL The expected latest value for field_2 is returned in history. | AssertionError: expected ‘Test Value Entry2’ to equal ‘Test Value Entry2 Change’

If I run this (only change is I added [0] to the vallue check line), the test only checks the first character of the value “T” rather than the entire value.

pm.test('The expected latest value for field_2 is returned in history.', function () {
    pm.expect(pm.response.json().history).to.be.an('object');
    Object.values(pm.response.json().history).forEach(history => {
        pm.expect(history).to.be.an('array');
        history.forEach(field => {
            pm.expect(field).to.have.property('fieldId');
            var field2 = (pm.collectionVariables.get('field2Id'));
            if (field.fieldId === field2) {
                pm.expect(field.value[0]).to.equal('Test Value Entry2 Change');
            }
        });
    });
});

Test Results:
The expected latest value for field_2 is returned in history. | AssertionError: expected ‘T’ to equal ‘Test Value Entry2 Change’

How can I modify this to only check the first instance of value: is equal to “Test Value Entry2 Change” within the field_2 array?

Thank you in advance!

I figured this out! I hope your time was not wasted. Thanks.
Here’s the solution in case anyone else is hunting the same:

pm.test('The expected latest value for field_2 is returned in history.', function () {
    var field2Id = pm.collectionVariables.get('field2Id');
    pm.expect(pm.response.json().history.field_2[0].value).to.equal('Test Value Entry2 Change');
});

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.