How to update environment variables based on a response in Postman

Written by ppolyzos

Software engineer based in Athens, Greece

8 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.

Leave a Comment

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