Saving object values in a variable from nested arrays from JSON response

[
    {
        "sku": "93303",
        "name": "Intex Pool-Solarplane Blau 732x366 cm Polyethylen",
        "categories": [
            {
                "id": 22740,
                "name": "Poolabdeckungen & -unterlagen",
                "slug": "poolabdeckungen-unterlagen"
            }
        ],
        "attributes": [
            {
                "id": 54,
                "name": "Farbe",
                "position": 1,
                "visible": true,
                "variation": false,
                "options": [
                    "Blau"
                ]
            },
            {
            {
                "id": 58,
                "name": "Marke",
                "position": 5,
                "visible": true,
                "variation": false,
                "options": [
                    "INTEX"
                ]
            },
        ],
        "meta_data": [
            {
                "id": 64852584,
                "key": "gtin_code",
                "value": "6941057422268"
            },
            {
                "id": 64852585,
                "key": "cost",
                "value": "54.19"
            },
    }
]

So, my response looks like this from above. I would like to store just 2 pieces of information in local variables from this response.

That data is:

  1. From ā€œattributesā€ array, from the following object:
 {
                "id": 58,
                "name": "Marke",
                "position": 5,
                "visible": true,
                "variation": false,
                "options": [
                    "INTEX"
                ]
 }

I would like to store just this ā€œINTEXā€ part.

  1. From ā€œmeta_dataā€ array, from the following object:
{
              "id": 64852584,
              "key": "gtin_code",
              "value": "6941057422268"
}

I would like to store this ā€œ6941057422268ā€ part.

How this could be done?

It is important to say that position of objects in both arrays could alter. For instance, in ā€œmeta_dataā€, in this specific case, the object we are interested in is on the [0] position, but it could be anywhere between [0] and [3].

That being said, what I have used for storing the ā€œcategoriesā€ (this:

const categoryPath = response.map(data => data.categories[0].name);

), can not be used for ā€œattributesā€ and ā€œmeta_dataā€. To understand what I am doing, please take a look at my test script:

const response = pm.response.json();

const sku = response.map(data => data.sku);
console.log(sku);
const title = response.map(data => data.name);
console.log(title);
const categoryPath = response.map(data => data.categories[0].name);
console.log(categoryPath);

pm.variables.set('skus4idealo', sku);
pm.variables.set('titles4idealo', title);
pm.variables.set('category4idealo', categoryPath);

So, how to add these 2 values to local variables?

Thanks in advance.

Hi @flight-explorer-5099

If you wanted to use a more static approach, you could use;

const response = pm.response.json();

console.log(response[0].attributes[1].options[0]);
console.log(response[0].meta_data[1].value);

If you wanted to go through each item in each array then you could use;

const response = pm.response.json();

for (let x = 0; x < response[0].attributes.length; x++) {
    console.log(response[0].attributes[x].options[0]);
}

for (let y = 0; y < response[0].meta_data.length; y++) {
    console.log(response[0].meta_data[y].value);
}

Also (there is incorrect formatting in your JSON, I had to change a couple of brackets to get it to work properly. Iā€™m assuming this was a copy and paste issue, but thought it worth mentioning).

Hi,

Thanks for getting back. I really appreciate the help.

(there is incorrect formatting in your JSON, I had to change a couple of brackets to get it to work properly. Iā€™m assuming this was a copy and paste issue, but thought it worth mentioning)

Yes. This could be. The real response is quite large, so it would be very hard to spot objects I need, so I discarded quite a bit of it to make things easier for understanding.

If you wanted to use a more static approach, you could use

Actually, this is what I know how to do, but it does not work, cos object positions are altering all the time, so I am storing incorect data in many cases.

If you wanted to go through each item in each array then you could use;

This is something close to that what I need, but it stores a lot of information instead of just one or 2 things. Maybe I do not know how to run it and to modify it.

The ideal case scenario would be following:

  1. Search the response for this value: ā€œMarkeā€
  2. If you find it, look in that object which contains this value ā€œMarkeā€ for a key called ā€œoptionsā€ and extract that keyā€™s value.

I am talking about this object:

{
           "id": 58,
           "name": "Marke",
           "position": 5,
           "visible": true,
           "variation": false,
           "options": [
              "INTEX"
                ]
}

So, in this case, only this word should be stored in a variable: ā€œINTEXā€.

How to achieve this?

const response = pm.response.json();

//Loop through all items
for (let x = 0; x < response[0].attributes.length; x++) {
    //Check if name is Marke
    if (response[0].attributes[x].name === "Marke"){
        //If true then do something
        console.log(response[0].attributes[x].options[0]);
    }
}

Awesome! We are almost there. Tchnicaly, this is what I need: storing just that little piece of info.

However, currentlly it stores that value just once, so I am not sure from which product it pulled that information.

In my response, I have 10 different products, so I need to make sure it looks for that ā€œMarkeā€ through every single product and stores that value just for the current product. Here is the screenshot how my results are looking

I think the best is to share the complete response and the test script with you, so you can run that from your side, but since there is a character limit here, I will do just 2 products.

So, this is the response which contains 2 products:

[
    {
        "sku": "91357",
        "name": "Schaukelset mit 5 Sitzen Orange",
        "permalink": "https://shopnetic.de/spielzeuge-spiele/spielzeug-fuer-draussen/spieltuerme/schaukelset-mit-5-sitzen-orange/",
        "regular_price": "235.03",
        "stock_quantity": 123,
        "categories": [
            {
                "id": 22038,
                "name": "SpieltĆ¼rme",
                "slug": "spieltuerme"
            }
        ],
        "attributes": [
            {
                "id": 54,
                "name": "Farbe",
                "position": 1,
                "visible": true,
                "variation": false,
                "options": [
                    "Orange"
                ]
            },
            {
                "id": 55,
                "name": "Geschlecht",
                "position": 2,
                "visible": true,
                "variation": false,
                "options": [
                    "Kinder"
                ]
            },
            {
                "id": 58,
                "name": "Marke",
                "position": 5,
                "visible": true,
                "variation": false,
                "options": [
                    "vidaXL"
                ]
            },
            {
                "id": 59,
                "name": "Art der Pakete",
                "position": 6,
                "visible": true,
                "variation": false,
                "options": [
                    "parcel"
                ]
            },
            {
                "id": 60,
                "name": "Anzahl der PackstĆ¼cke",
                "position": 7,
                "visible": true,
                "variation": false,
                "options": [
                    "1"
                ]
            }
        ],
        "meta_data": [
            {
                "id": 69383907,
                "key": "_alg_wc_cog_profit",
                "value": "197.504202"
            },
            {
                "id": 69383908,
                "key": "_alg_wc_cog_profit_percent",
                "value": "0"
            },
            {
                "id": 69383909,
                "key": "_alg_wc_cog_profit_margin",
                "value": "100"
            },
            {
                "id": 69383929,
                "key": "fifu_list_url",
                "value": "https://vdxl.im/8718475571131_a_en_hd_1.jpg|https://vdxl.im/8718475571131_g_en_hd_1.jpg|https://vdxl.im/8718475571131_g_en_hd_2.jpg|https://vdxl.im/8718475571131_g_en_hd_3.jpg|https://vdxl.im/8718475571131_g_en_hd_4.jpg|https://vdxl.im/8718475571131_g_en_hd_5.jpg|https://vdxl.im/8718475571131_g_en_hd_6.jpg|https://vdxl.im/8718475571131_g_en_hd_7.jpg|https://vdxl.im/8718475571131_g_en_hd_8.jpg|https://vdxl.im/8718475571131_g_en_hd_9.jpg|https://vdxl.im/8718475571131_g_en_hd_10.jpg|https://vdxl.im/8718475571131_g_en_hd_11.jpg"
            },
            {
                "id": 69383930,
                "key": "fifu_list_alt",
                "value": "Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange|Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383931,
                "key": "fifu_image_url",
                "value": "https://vdxl.im/8718475571131_a_en_hd_1.jpg"
            },
            {
                "id": 69383932,
                "key": "fifu_image_alt",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383933,
                "key": "fifu_image_url_0",
                "value": "https://vdxl.im/8718475571131_g_en_hd_1.jpg"
            },
            {
                "id": 69383934,
                "key": "fifu_image_alt_0",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383935,
                "key": "fifu_image_url_1",
                "value": "https://vdxl.im/8718475571131_g_en_hd_2.jpg"
            },
            {
                "id": 69383936,
                "key": "fifu_image_alt_1",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383937,
                "key": "fifu_image_url_2",
                "value": "https://vdxl.im/8718475571131_g_en_hd_3.jpg"
            },
            {
                "id": 69383938,
                "key": "fifu_image_alt_2",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383939,
                "key": "fifu_image_url_3",
                "value": "https://vdxl.im/8718475571131_g_en_hd_4.jpg"
            },
            {
                "id": 69383940,
                "key": "fifu_image_alt_3",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383941,
                "key": "fifu_image_url_4",
                "value": "https://vdxl.im/8718475571131_g_en_hd_5.jpg"
            },
            {
                "id": 69383942,
                "key": "fifu_image_alt_4",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383943,
                "key": "fifu_image_url_5",
                "value": "https://vdxl.im/8718475571131_g_en_hd_6.jpg"
            },
            {
                "id": 69383944,
                "key": "fifu_image_alt_5",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383945,
                "key": "fifu_image_url_6",
                "value": "https://vdxl.im/8718475571131_g_en_hd_7.jpg"
            },
            {
                "id": 69383946,
                "key": "fifu_image_alt_6",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383947,
                "key": "fifu_image_url_7",
                "value": "https://vdxl.im/8718475571131_g_en_hd_8.jpg"
            },
            {
                "id": 69383948,
                "key": "fifu_image_alt_7",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383949,
                "key": "fifu_image_url_8",
                "value": "https://vdxl.im/8718475571131_g_en_hd_9.jpg"
            },
            {
                "id": 69383950,
                "key": "fifu_image_alt_8",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383951,
                "key": "fifu_image_url_9",
                "value": "https://vdxl.im/8718475571131_g_en_hd_10.jpg"
            },
            {
                "id": 69383952,
                "key": "fifu_image_alt_9",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383953,
                "key": "fifu_image_url_10",
                "value": "https://vdxl.im/8718475571131_g_en_hd_11.jpg"
            },
            {
                "id": 69383954,
                "key": "fifu_image_alt_10",
                "value": "Schaukelset mit 5 Sitzen Orange"
            },
            {
                "id": 69383955,
                "key": "_rank_math_gtin_code",
                "value": "8718475571131"
            },
            {
                "id": 69383956,
                "key": "_alg_wc_cog_cost",
                "value": "145.98"
            },
            {
                "id": 69383957,
                "key": "_wp_page_template",
                "value": "default"
            },
            {
                "id": 69383958,
                "key": "rank_math_internal_links_processed",
                "value": "1"
            },
            {
                "id": 69483263,
                "key": "fifu_tmp_product_image_gallery",
                "value": "10633985,10633986,10633987,10633988,10633989,10633990,10633991,10633992,10633993,10633994,10633995"
            }
        ],
        "delivery_time": {
            "name": "3ā€“6 Werktage"
        }
    },
    {
        "sku": "809515",
        "name": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte",
        "permalink": "https://shopnetic.de/moebel/tische/nachttische/nachttische-2-stk-schwarz-45x34x44-cm-spanplatte/",
        "regular_price": "63.98",
        "stock_quantity": 140,
        "categories": [
            {
                "id": 21525,
                "name": "Nachttische",
                "slug": "nachttische"
            }
        ],
        "attributes": [
            {
                "id": 54,
                "name": "Farbe",
                "position": 1,
                "visible": true,
                "variation": false,
                "options": [
                    "Schwarz"
                ]
            },
            {
                "id": 57,
                "name": "GrĆ¶ĆŸe",
                "position": 4,
                "visible": true,
                "variation": false,
                "options": [
                    "N/A"
                ]
            },
            {
                "id": 58,
                "name": "Marke",
                "position": 5,
                "visible": true,
                "variation": false,
                "options": [
                    "vidaXL"
                ]
            },
            {
                "id": 59,
                "name": "Art der Pakete",
                "position": 6,
                "visible": true,
                "variation": false,
                "options": [
                    "parcel"
                ]
            },
            {
                "id": 60,
                "name": "Anzahl der PackstĆ¼cke",
                "position": 7,
                "visible": true,
                "variation": false,
                "options": [
                    "1"
                ]
            }
        ],
        "meta_data": [
            {
                "id": 69278794,
                "key": "_alg_wc_cog_profit",
                "value": "53.764706"
            },
            {
                "id": 69278795,
                "key": "_alg_wc_cog_profit_percent",
                "value": "0"
            },
            {
                "id": 69278796,
                "key": "_alg_wc_cog_profit_margin",
                "value": "100"
            },
            {
                "id": 69278816,
                "key": "fifu_list_url",
                "value": "https://vdxl.im/8720286834527_m_en_hd_1.jpg|https://vdxl.im/8720286834527_a_en_hd_1.jpg|https://vdxl.im/8720286834527_g_en_hd_1.jpg|https://vdxl.im/8720286834527_g_en_hd_2.jpg|https://vdxl.im/8720286834527_g_en_hd_3.jpg|https://vdxl.im/8720286834527_g_en_hd_4.jpg|https://vdxl.im/8720286834527_g_en_hd_5.jpg|https://vdxl.im/8720286834527_g_en_hd_6.jpg|https://vdxl.im/8720286834527_g_en_hd_7.jpg|||"
            },
            {
                "id": 69278817,
                "key": "fifu_list_alt",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte|Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278818,
                "key": "fifu_image_url",
                "value": "https://vdxl.im/8720286834527_m_en_hd_1.jpg"
            },
            {
                "id": 69278819,
                "key": "fifu_image_alt",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278820,
                "key": "fifu_image_url_0",
                "value": "https://vdxl.im/8720286834527_a_en_hd_1.jpg"
            },
            {
                "id": 69278821,
                "key": "fifu_image_alt_0",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278822,
                "key": "fifu_image_url_1",
                "value": "https://vdxl.im/8720286834527_g_en_hd_1.jpg"
            },
            {
                "id": 69278823,
                "key": "fifu_image_alt_1",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278824,
                "key": "fifu_image_url_2",
                "value": "https://vdxl.im/8720286834527_g_en_hd_2.jpg"
            },
            {
                "id": 69278825,
                "key": "fifu_image_alt_2",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278826,
                "key": "fifu_image_url_3",
                "value": "https://vdxl.im/8720286834527_g_en_hd_3.jpg"
            },
            {
                "id": 69278827,
                "key": "fifu_image_alt_3",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278828,
                "key": "fifu_image_url_4",
                "value": "https://vdxl.im/8720286834527_g_en_hd_4.jpg"
            },
            {
                "id": 69278829,
                "key": "fifu_image_alt_4",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278830,
                "key": "fifu_image_url_5",
                "value": "https://vdxl.im/8720286834527_g_en_hd_5.jpg"
            },
            {
                "id": 69278831,
                "key": "fifu_image_alt_5",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278832,
                "key": "fifu_image_url_6",
                "value": "https://vdxl.im/8720286834527_g_en_hd_6.jpg"
            },
            {
                "id": 69278833,
                "key": "fifu_image_alt_6",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278834,
                "key": "fifu_image_url_7",
                "value": "https://vdxl.im/8720286834527_g_en_hd_7.jpg"
            },
            {
                "id": 69278835,
                "key": "fifu_image_alt_7",
                "value": "Nachttische 2 Stk. Schwarz 45x34x44 cm Spanplatte"
            },
            {
                "id": 69278836,
                "key": "_rank_math_gtin_code",
                "value": "8720286834527"
            },
            {
                "id": 69278837,
                "key": "_alg_wc_cog_cost",
                "value": "39.74"
            },
            {
                "id": 69278838,
                "key": "_wp_page_template",
                "value": "default"
            },
            {
                "id": 69278839,
                "key": "rank_math_internal_links_processed",
                "value": "1"
            }
        ],
        "delivery_time": {
            "name": "3ā€“6 Werktage"
        }
    },
]

And this is the test script:

const response = pm.response.json();

const sku = response.map(data => data.sku);
console.log(sku);
const title = response.map(data => data.name);
console.log(title);
const price = response.map(data => data.regular_price);
console.log(price);
const url = response.map(data => data.permalink);
console.log(url);
const delivery = response.map(data => data.delivery_time.name);
console.log(delivery);
const checkoutLimitPerPeriod = response.map(data => data.stock_quantity);
console.log(checkoutLimitPerPeriod);
const categoryPath = response.map(data => data.categories[0].name);
console.log(categoryPath);

//Loop through all items
for (let x = 0; x < response[0].attributes.length; x++) {
    //Check if name is Marke
    if (response[0].attributes[x].name === "Marke"){
        //If true then do something
        console.log(response[0].attributes[x].options[0]);
    }
}



pm.variables.set('skus4idealo', sku);
pm.variables.set('titles4idealo', title);
pm.variables.set('prices4idealo', price);
pm.variables.set('urls4idealo', url);
pm.variables.set('delivery4idealo', delivery);
pm.variables.set('category4idealo', categoryPath);
pm.variables.set('stock4idealo', checkoutLimitPerPeriod);

try this;

const response = pm.response.json();

const sku = response.map(data => data.sku);
console.log(sku);
const title = response.map(data => data.name);
console.log(title);
const price = response.map(data => data.regular_price);
console.log(price);
const url = response.map(data => data.permalink);
console.log(url);
const delivery = response.map(data => data.delivery_time.name);
console.log(delivery);
const checkoutLimitPerPeriod = response.map(data => data.stock_quantity);
console.log(checkoutLimitPerPeriod);
const categoryPath = response.map(data => data.categories[0].name);
console.log(categoryPath);

for (let x = 0; x < response.length; x++) 
{
    //Loop through all items
    for (let y = 0; y < response[x].attributes.length; y++) 
    {
        //Check if name is Marke
        if (response[x].attributes[y].name === "Marke")
        {
        //If true then do something
        console.log("category; " + response[x].categories[0].name + "   has attribute; " + response[x].attributes[y].name + "   and the option is; " + response[x].attributes[y].options[0]);
        }
    }
}

Or this if you want to see the output of the ones that donā€™t match too;

const response = pm.response.json();

const sku = response.map(data => data.sku);
console.log(sku);
const title = response.map(data => data.name);
console.log(title);
const price = response.map(data => data.regular_price);
console.log(price);
const url = response.map(data => data.permalink);
console.log(url);
const delivery = response.map(data => data.delivery_time.name);
console.log(delivery);
const checkoutLimitPerPeriod = response.map(data => data.stock_quantity);
console.log(checkoutLimitPerPeriod);
const categoryPath = response.map(data => data.categories[0].name);
console.log(categoryPath);

for (let x = 0; x < response.length; x++) 
{
    //Loop through all items
    for (let y = 0; y < response[x].attributes.length; y++) 
    {
        //Check if name is Marke
        if (response[x].attributes[y].name === "Marke")
        {
        //If true then do something
        console.log("category; " + response[x].categories[0].name + " has attribute; " + response[x].attributes[y].name + "   and the option is; " + response[x].attributes[y].options[0]);
        }
        else
        {
            console.log("category; " + response[x].categories[0].name + " has attribute; " + response[x].attributes[y].name + " = NO MATCH");
        }
    }
}

Hi,

Yes. The first one does the job. I am doing this:

const response = pm.response.json();

const sku = response.map(data => data.sku);
console.log(sku);
const title = response.map(data => data.name);
console.log(title);
const price = response.map(data => data.regular_price);
console.log(price);
const url = response.map(data => data.permalink);
console.log(url);
const delivery = response.map(data => data.delivery_time.name);
console.log(delivery);
const checkoutLimitPerPeriod = response.map(data => data.stock_quantity);
console.log(checkoutLimitPerPeriod);
const categoryPath = response.map(data => data.categories[0].name);
console.log(categoryPath);

for (let x = 0; x < response.length; x++) 
{
    //Loop through all items
    for (let y = 0; y < response[x].attributes.length; y++) 
    {
        //Check if name is Marke
        if (response[x].attributes[y].name === "Marke")
        {
        //If true then do something
        console.log(response[x].attributes[y].options[0]);
        }
    }
}

for (let x = 0; x < response.length; x++) 
{
    //Loop through all items
    for (let y = 0; y < response[x].meta_data.length; y++) 
    {
        //Check if name is Marke
        if (response[x].meta_data[y].key === "fifu_image_url")
        {
        //If true then do something
        console.log(response[x].meta_data[y].value);
        }
    }
}

for (let x = 0; x < response.length; x++) 
{
    //Loop through all items
    for (let y = 0; y < response[x].meta_data.length; y++) 
    {
        //Check if name is Marke
        if (response[x].meta_data[y].key === "_rank_math_gtin_code")
        {
        //If true then do something
        console.log(response[x].meta_data[y].value);
        }
    }
}

pm.variables.set('skus4idealo', sku);
pm.variables.set('titles4idealo', title);
pm.variables.set('prices4idealo', price);
pm.variables.set('urls4idealo', url);
pm.variables.set('delivery4idealo', delivery);
pm.variables.set('category4idealo', categoryPath);
pm.variables.set('stock4idealo', response[x].attributes[y].options[0]);
pm.variables.set('image4idealo', response[x].meta_data[y].value);
pm.variables.set('ean4idealo', response[x].meta_data[y].value);

But I have no idea how to store extracted values in a local variable. I mean, just this part you have written. How to do that?

instead of (or as well as) writing to the console like this;

console.log(response[x].attributes[y].options[0]);

you can use something like this to save the value as a collection variable;

pm.collectionVariables.set("option", response[x].attributes[y].options[0]);

That is exactly what I did, but I am getting ā€œReferenceError: x is not definedā€.

These are executed outside of your ā€˜forā€™ loopā€¦ but x is declared inside the ā€˜forā€™ loop.

Therefore ā€˜xā€™ is not defined when you are trying to use it at the bottom of your script.


Iā€™m not sure what you are trying to achieve with these variable set statements though, as the variables would be repeatedly overwritten until the last loop.

Meaning that once all of the for loop iterations are complete, only the values from the very last loop will be stored in these variables.

Exactly! That is what I am doing. I am trying to send that product data to a marketplace. So, I need fresh data every time.

Are there any chances to do this how I did it for values before arrays kicked in? With map etc.?