Extract all object from array with testscripts

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hereโ€™s an outline with best practices for making your inquiry.

My question:
How do I extract all product_id from this json file?
I had try on some related post with same question but I can not find the results as expected.

Details (like screenshots):

{
    "code": 200,
    "message": "Success",
    "data": {
        "data_item": [{
                "name": "A",
                "prefixes": [
                    "p1",
                    "p2"
                ],
                "products": [{
                        "product_id": 2442793,
                        "name": "BBB",
                    }, {
                        "product_id": 2443085,
                        "name": "AAA",
                    }, {
                        "product_id": 2443089,
                        "name": "90A",
                    }, {
                        "product_id": 2443091,
                        "name": "VVV",

                    }
                ]
            }, {
                "name": "B",
                "prefixes": [
                    "p1",
                    "p2",
                ],
                "products": [{
                        "product_id": 24427933421,
                        "name": "VB",
                    }, {
                        "product_id": 24434373332,
                        "name": "GG",
                    }, {
                        "product_id": 2443094333,
                        "name": "ABC",
                    }, {
                        "product_id": 244332095,
                        "name": "1da",
                    }, {
                        "product_id": 244435937,
                        "name": "KA",
                    }
                ]
            }, {
                "name": "C",
                "prefixes": [
                    "p1",
                    "p2",
                ],
                "products": [{
                        "product_id": 232499,
                        "name": "10A",
                    }, {
                        "product_id": 42643101,
                        "name": "20A",
                    }, {
                        "product_id": 24443503,
                        "name": "30A",
                    }, {
                        "product_id": 244435105,
                        "name": "40A",
                    }, {
                        "product_id": 244345107,
                        "name": "50A",
                    }
                ]
            }
        }
    }

How I found the problem:

Iโ€™ve already tried:
const response = pm.response.json();

let lstprod =

_.each(lstproducts.data_item, (result) => {

lstprod.push(result.products);

})
const Ids = lstprod .map(x => x.product_id)

Hi @hugobnt88,

You may need to use nested for each or _.each per your problem statement.

Below is the snippet which can extract as per the JSON response.

var respBody= pm.response.json();
console.log(respBody);
let productIds=[]
_.each(respBody.data.data_item, (dataIt)=>{
    _.each(dataIt.products,(product)=>{
        productIds.push(product.product_id)
    })
});
console.log(productIds);

I hope this helps :slight_smile:

1 Like

Itโ€™s works, Thank you so much @pranavdavar.