# Help with parsing and setting an environment variable

**URL:** <https://community.postman.com/t/help-with-parsing-and-setting-an-environment-variable/49560>\
**Category:** Help Hub\
**Tags:** variables, collections, json\
**Created:** [July 14, 2023, 3:43pm UTC](https://community.postman.com/t/help-with-parsing-and-setting-an-environment-variable/49560 "2023-07-14T15:43:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![julio-p](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@julio-p](https://community.postman.com/u/julio-p)\
**Post date:** [July 14, 2023, 3:43pm UTC](https://community.postman.com/t/help-with-parsing-and-setting-an-environment-variable/49560/1 "2023-07-14T15:43:02Z")

</div>

Hi, Can anyone help me and provide the tests script to parse the first item of many in an API response message? I am trying to parse the 1st element value called itineraryId in this API response message and save it to an environment variable but having difficulty. I am able to parse the Search ID item successfully but the sub item of Itinerary ID is causing me issues.

_Sample API response message_:  
{  
“searchId”: “XXXXXXXXXXXXI=”,  
“itineraryDetails”: {  
“itineraries”: [  
{  
“itineraryId”: “CicKJQoQYmNkYmFlNzU0YmRmYTc0YhIPCA4SATEYASIGCgRMQTVLIAE=”,

**I’ve already tried** \*:  
This for the Search ID and it works but not sure how to parse and save the 1st Itinerary ID to a environment file object.

var jsonData = JSON.parse(responseBody);

postman.setEnvironmentVariable(“Search ID”, jsonData.searchId);

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [July 17, 2023, 10:08am UTC](https://community.postman.com/t/help-with-parsing-and-setting-an-environment-variable/49560/2 "2023-07-17T10:08:08Z")

</div>

Itineraries is an array so you need to include the array index (array indexes start at zero).

```auto
const response = pm.response.json();
console.log(response);

let itinerary = response.itineraryDetails.itineraries[0].itineraryId
console.log(itinerary);

pm.environment.set("itineraryId", itinerary);
console.log(pm.environment.get("itineraryId"));

```

---

<div class="post-metadata">

**Author:** ![julio-p](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@julio-p](https://community.postman.com/u/julio-p)\
**Post date:** [July 17, 2023, 2:11pm UTC](https://community.postman.com/t/help-with-parsing-and-setting-an-environment-variable/49560/3 "2023-07-17T14:11:38Z")

</div>

@michaelderekjones Thanks so much for the help, that code works and has resolved my question.
