Generate variables from responding body with duplicate id´s

Hi
I am trying to generate two variables from this body

{
    "Cisco-IOS-XE-wireless-wlan-cfg:mpsk-keys": {
        "mpsk-key": [
            {
                "priority": 1,
                "mpsk-key": "DODATEST1",
                "mpsk-key-format": "key-ascii"
            },
            {
                "priority": 2,
                "mpsk-key": "DODATEST02",
                "mpsk-key-format": "key-ascii"
            }
        ]

i want to get the values DODATEST01 and DODATEST02 to generate variables to change them later in another postman command.
I think the challenge here is the multiple keyword “mpsk-key” that prevails to generate a variable for each entry because there are multiple matches ?

Maybe someone had a similar problem in the past and have already a Solution for that ?
bye Markus

You don’t really say how you are going to change these variables later or how you need to consume them in the subsequent requests.

The following is based on an assumption that you need individual variables for each key.

You need to define the mpsk-key array as a variable and then loop through the array.

One of the issues you have is the hyphens, so you can’t target the elements using the traditional dot notation.

You can use the array index to customize the variable name so its unique. (Or if the priority is unique, I would use that instead).

This uses backticks and a JavaScript feature called string literals.

The following should work against your example response.

const response = pm.response.json();

let mpskKeys = response["Cisco-IOS-XE-wireless-wlan-cfg:mpsk-keys"]["mpsk-key"]
// console.log(mpskKeys);

mpskKeys.forEach((obj, index) => {
    let mpskKey = obj["mpsk-key"]
    // console.log(mpskKey);
    pm.collectionVariables.set(`mpsk-key_${index}`, mpskKey);
});

image

1 Like

Hey @amannma :wave:

Welcome to the Postman Community! :postman:

You should be able to loop over the data and extract those values from the keys inside the array. There are a number of different topics on the forum that will give you some solutions of how to do this - This is more of a Javascript problem than a specific Postman problem, learning a few basics of that language will help you to create scripts in Postman with ease.


Alternatively, With the introduction of Postbot, you have the capability to ask it those types of questions. Here’s I’m using a basic prompt that returning me some basic code that I could use to do this. I’m using the Postman Echo service here so some of the references to the response data would be different on your side.

// Get the data object from the response
data = pm.response.json().data;

// Extract the values of the mpsk-key keys from the mpsk-key array
mpskKeys = data['Cisco-IOS-XE-wireless-wlan-cfg:mpsk-keys']['mpsk-key'];

// Store the mpsk-key values in individual global variables
mpskKeys.forEach(function(key, index) {
    pm.globals.set("mpskKeyValue_" + index, key['mpsk-key']);
});

This is making those values individual variables but this could be amended to push all the values into an array, which would make it easier to use them in other requests, as it would be under a single variable key and not loads of unique global variable names.

Can we ask Postbot why its using global variables :slight_smile:

In this case, it’s doing exactly what I told it too do :joy:

But honestly, that would be a great hint or suggestion “Are you sure that’s the right variable scope that you should be using here?

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