How to check data type of nested array elements

Hi ,

I want to check data type of below nested array elements i.e. for “id”

{
    "name": "Test1",
    "values": [
        {
            "id": "1",
            "value": "Value1",
            "values": [
                {
                    "id": "1",
                    "value": "ABC-1",
                    "values": null
                },
                {
                    "id": "1",
                    "value": "ABC-2",
                    "values": null
                },
                {
                    "id": "1",
                    "value": "ABC-3",
                    "values": null
                },
               ]
        },
        {
            "id": "2",
            "value": "Value2",
            "values": [
                {
                    "id": "2",
                    "value": "PQR1",
                    "values": null
                },
                {
                    "id": "2",
                    "value": "PQR2",
                    "values": null
                },
                {
                    "id": "2",
                    "value": "PQR3",
                    "values": null
                }
            ]
        },

Urgent help will be appreciable. Thanks in advance

Hey @rohit2330

Welcome to the community! :wave:

The quickest way that I can think of is to do a loop through the top-level array and then loop through the nested array:

pm.test('Check nested Id data type', () => {
    _.each(pm.response.json().values, (topLevelItem) => {
        _.each(topLevelItem.values, (nestedItem) => {
            pm.expect(nestedItem.id).to.be.a('string')
        })
    })
})  

This is not a final solution and should really be refactored to make the code more robust and tolerant to different conditions but as you wanted something quick…here you go :slight_smile:

Thank you so much @dannydainton for your quick help.

1 Like