JSON Data and Loops

Hi,

I have an API that responds with the following format. I need to extract the data of the OPEN case. However, i cannot use the array index as this grows everytime there is a new entry made. Can someone advise on how i can grab the data in the OPEN node:

"head": {},

    "operations": [

        [

            "destroy",

            "w1232"

        ],

        [

            "destroy",

            "w1243"

        ],

        [

            "destroy",

            "w1242"

        ],

        [

            "destroy",

            "w1241"

        ],

        [

            "destroy",

            "w1240"

        ],

        [

            "destroy",

            "w1239"

        ],

        [

            "destroy",

            "w1238"

        ],

        [

            "destroy",

            "w1237"

        ],

        [

            "destroy",

            "w1236"

        ],

        [

            "destroy",

            "w1235"

        ],

        [

            "destroy",

            "w1234"

        ],

        [

            "destroy",

            "w1233"

        ],

        [

            "set",

            "w571",

            {

                "image": [

                    "rwt-resources/generated/45762366.gif",

                    16,

                    16

                ]

            }

        ],

        [

            "set",

            "w630",

            {

                "image": [

                    "rwt-resources/generated/45762366.gif",

                    16,

                    16

                ]

            }

        ],

        [

            "set",

            "w1194",

            {

                "image": [

                    "rwt-resources/generated/45762366.gif",

                    16,

                    16

                ],

                "activeControl": "w1216"

            }

        ],

        [

            "set",

            "w1212",

            {

                "itemCount": 13

            }

        ],

        [

            "create",

            "w1247",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 0,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Open",

                    "251aa827-8b1c-4050-bb30-f85b084ae390",

                    "20-Aug-2020 10:25",

                    "",

                    ""

                ]

            }

        ],

        [

            "create",

            "w1248",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 1,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Closed",

                    "825c995f-7e75-453b-9648-4dacf8f542e1",

                    "20-Aug-2020 10:22",

                    "20-Aug-2020 10:24",

                    "Completed"

                ]

            }

        ],

        [

            "create",

            "w1249",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 2,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Closed",

                    "2016fec4-fbb7-40a1-b824-4eea5045d16c",

                    "20-Aug-2020 10:13",

                    "20-Aug-2020 10:21",

                    "Completed"

                ]

            }

        ],

        [

            "create",

            "w1250",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 3,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Closed",

                    "19a6c126-a38c-4f41-831d-d49ab19cd93d",

                    "20-Aug-2020 10:11",

                    "20-Aug-2020 10:12",

                    "Completed"

                ]

            }

        ],

        [

            "create",

            "w1251",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 4,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Closed",

                    "1d38faaa-26bf-4f10-a16c-47329d90f896",

                    "20-Aug-2020 09:31",

                    "20-Aug-2020 09:35",

                    "Completed"

                ]

            }

        ],

        [

            "create",

            "w1252",

            "rwt.widgets.GridItem",

            {

                "parent": "w1212",

                "index": 5,

                "texts": [

                    "Standard (with Payment Deferral Request)",

                    "Closed",

                    "ea57f9b5-286a-4f16-b02e-abd90146a111",

                    "20-Aug-2020 09:21",

                    "20-Aug-2020 09:21",

                    "Completed"

                ]

            }

        ],

        [

Is there a way to find Index[0] and then get the Text node?

You can use this javascript snippet to find the operation with a subitem that has open.

 const items = data.operations.filter(d => d.some(s => s.hasOwnProperty('texts') && s.texts.find(t => t.toLowerCase() === 'open')));

Are you able to change the output of this API? Converting it to json instead of arrays that contain a hodgepodge of objects would help tremendously.

Unfortunately not, this is what i have got to play with :frowning: And i need to always get the 1st instance in the array. However, as mentioned, as a new entry goes in, im left guessing what the array number will be

If you want the first one, you can modify my script to this

const item = data.operations.find(d => d.some(s => s.hasOwnProperty('texts') && s.texts.find(t => t.toLowerCase() === 'open')));

Hey @allenheltondev

If i try either of those, Postman throws an error that “filter” is undefined / “find” is undefined.

 [

        "create",

        "w1247",

        "rwt.widgets.GridItem",

        {

            "parent": "w1212",

            "index": 0,

            "texts": [

                "Standard (with Payment Deferral Request)",

                "Open",

                "251aa827-8b1c-4050-bb30-f85b084ae390",

                "20-Aug-2020 10:25",

                "",

                ""

            ]

        }

    ],

I’ve had no lick trying to find

        "index": 0,

and then access the

texts": [

            "Standard (with Payment Deferral Request)",

            "Open",

            "251aa827-8b1c-4050-bb30-f85b084ae390",

            "20-Aug-2020 10:25",

            "",

            ""

        ]

data :frowning:

Oh, data was the variable I was using to test your scenario. This should apply to you:

const jsonData = pm.response.json();
const item = jsonData.operations.find(d => d.some(s => s.hasOwnProperty('texts') && s.texts.find(t => t.toLowerCase() === 'open')));

That should go in the tests section of your requestion.

If that doesn’t work, you might need to show us the exact payload of the response again.