POSTMAN - Save a property value from a JSON response

Viewed 6893

I am new to both JSON and Postman(as of yesterday).

I'm trying to do something very simple, I've created a GET request which pulls in a list of forms in a JSON response. I want to take this response and get the first "id" token and place it in a variable.

I am using a global variable but would like to use a collection variable if possible. Either way here is what I am doing.

I've tried several things, most recently this:

var jsonData = JSON.parse(responseBody);
postman.setGlobalVariable("id", jsonData.args.id);

As well as this:

pm.test("GetId", function () {
    var jsonData = pm.response.json();
    pm.globals.set("id", jsonData.id);    
});

Response code looks like this:

{
   "forms":[
      {
         "id":"3197239",
         "created":"2018-09-18 11:37:39",
         "db":"1",
         "deleted":"0",
         "folder":"151801",
         "language":"en",
         "name":"Contact Us",
         "num_columns":"2",
         "submissions":"0",
         "submissions_unread":"0",
         "updated":"2018-09-18 12:02:13",
         "viewkey":"xxxxxx",
         "views":"1",
         "submissions_today":0,
         "url":"https://xxx",
         "data_url":"",
         "summary_url":"",
         "rss_url":"",
         "encrypted":false,
         "thumbnail_url":null,
         "submit_button_title":"Submit Form",
         "inactive":false,
         "timezone":"US/Eastern",
         "permissions":150
      },
      {
         "id":"3197245",
         "created":"2018-09-18 11:42:02",
         "db":"1",
         "deleted":"0",
         "folder":"151801",
         "language":"en",
         "name":"Football Draft",
         "num_columns":"1",
         "submissions":"0",
         "submissions_unread":"0",
         "updated":"2018-09-18 12:11:54",
         "viewkey":"xxxxxx",
         "views":"1",
         "submissions_today":0,
         "url":"https://xxxxxxxxx",
         "data_url":"",
         "summary_url":"",
         "rss_url":"",
         "encrypted":false,
         "thumbnail_url":null,
         "submit_button_title":"Submit Form",
         "inactive":false,
         "timezone":"US/Eastern",
         "permissions":150
      }
   ]
}
2 Answers

This would get the first id:

pm.globals.set('firstId', _.first(pm.response.json().forms).id)

That would get the first in the array each time so it would set a different variable it that response changed.


The test that you created was nearly there but the reference needed to go down a level into the forms array:

pm.test("GetId", function () {
    var jsonData = pm.response.json()
    pm.expect(jsonData.forms[0].id).to.equal("3197239")
    pm.globals.set("id", jsonData.forms[0].id)   
})

The [0]is referencing the first id in the first object within the array. For example [1] would get the second one and so on.


You currently cannot set a collection level variable using the pm.* API - These can only be added manually and referenced using the pm.variables.get('var_name') syntax.

Edit:

In the new versions of the desktop app you can set variables at the Collection level using pm.collectionVariables.set().

Based on the name or any other attribute if you want to set the id as a global variable then this is the way.


for(var i=0; i<jsonData.forms.length; i++)
{
    if (jsonData.forms[i].name==="Contact Us")
    {
       pm.environment.set("id", jsonData.forms[i].id);
    }   

}
Related