How to update environment variables based on a response in Postman

Written by ppolyzos

Software engineer based in Athens, Greece

17 Comments

  1. chenna

    How do you select 001-0000084765 from below JSon response.

    {
    “PageOfResults”: [
    {
    “RoleId”: 8,
    “Account”: {
    “HierarchyId”: “001-0000084765”,
    “AccountId”: “NT9041360”,
    “Name”: {
    “Full”: “Mr Philip ppppppppppppppp Lee”,
    “Medium”: null,
    “Short”: null,
    “Preferred”: null
    },
    “Type”: “Individual”,
    “Status”: “Active”,
    “DateCreated”: “2016-08-29”,
    “ExternalAccountId”: null,
    “Links”: null
    },
    “Customer”: {
    “CustomerId”: 14293,
    “Status”: “Active”,
    “PersonalDetails”: {
    “Name”: {
    “Title”: “Mr”,
    “Given”: “Philip”,
    “Middle”: “ppppppppppppppp”,
    “Family”: “Lee”,
    “Suffix”: null,
    “Salutation”: null
    },
    “MaritalStatus”: “NotSpecified”,
    “Gender”: “Male”,
    “GovernmentIdentifiers”: [
    {
    “Value”: “”,
    “Type”: “TaxFileNumber”,
    “Country”: “AUS”
    }
    ],
    “DateOfBirth”: “1980-03-01”,
    “Nationality”: “NotSpecified”,
    “CountryOfResidency”: “NotSpecified”,
    “TownOfBirth”: null,
    “Countries”: [
    {
    “Type”: “TaxCountryOfResidence”,
    “Country”: “AUS”,
    “TaxNumber”: “”
    }
    ],
    “Links”: null
    },
    “ExternalCustomerId”: null,
    “HierarchyId”: “015-0000014293”,
    “UserId”: 114591,
    “Links”: null
    },
    “Role”: “Individual”,
    “Links”: null
    }
    ],
    “TotalNumberOfResults”: 1,
    “Links”: null
    }

    Tried this code but doesn’t work

    var jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable(“HierarchyId”,jsondata.HierarchyId);

    1. ppolyzos

      To access hierarchyId you can do the following:

      One thing that might help you is Postman console. You can open it from View -> Show Postman Console and then you use console.log to help you. More info here

  2. Jhuang

    how to parse an xml response:

    10:00:c4:f5:7c:52:2a:18

    I tried this and it did not like the last response.fibrechannel-switch.name:
    var jsonData = xml2Json(responseBody);
    var response = JSON.parse(jsonData);
    postman.setEnvironmentVariable(“switchName”, response.fibrechannel-switch.name);

    thanks.

    1. ppolyzos

      To view the output of your jsonData variable you can use Postman Console. Open it from View -> Show Postman Console and then you use console.log to help you.
      Usually the parsing is a little different than you might expect, but with Console you ‘ll figure out the problem easily.

  3. niti

    How to get “Qk4oSI” from the response:

  4. Cain

    How would I get BackCustomerID? I’ve tried:

    1. ppolyzos

      You response is an array, so you need to get the first element:

    1. ppolyzos

      You can include those conditions in your Tests script.

  5. kanna

    {
    “serial”: “123456”,
    “headers”: {
    “Authorization”: “Signature keys=\”/smarId/hello/1234.123445\”,signature=\”www.hello.com/””
    }

    I want only */smarId/hello/1234.123445* as environment variable

    1. ppolyzos

      If only the Signature keys change, then you can do the following:

      You can learn more about string interpolation and template literals in this link

Leave a Comment

Your email address will not be published. Required fields are marked *