# Retrive data from json response

**URL:** <https://community.postman.com/t/retrive-data-from-json-response/16322>\
**Category:** Help Hub\
**Created:** [September 29, 2020, 10:12am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322 "2020-09-29T10:12:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 29, 2020, 10:12am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/1 "2020-09-29T10:12:06Z")

</div>

Hi Im having trouble rtrieving an integer value from a json respone and putting it into a variable

example of the json

{  
“data”: {  
“2020-09-29”: {  
“37677760”: [  
{  
“Code”: 11111111,  
“Type”: “data1”,  
“CodeDate”: “2020-09-29”,  
“Time”: “08:30”,  
“ADate”: “29 Sep 2020”,  
“data2”: 1,  
“data3”: 45,  
“data4”: 3766077777,  
“sata5”: 127,  
“Status”: “Past”,  
“data6”: 2110738111,  
},

i need to retrieve data4  
currently I have

let response = pm.response.json(), savedData = JSON.stringify(response);

pm.environment.set(“savedData”, savedData);

This stores the whole response

many thanks

---

<div class="post-metadata">

**Author:** ![kamalshree2784](https://avatars.discourse-cdn.com/v4/letter/k/f07891/32.png) [@kamalshree2784](https://community.postman.com/u/kamalshree2784)\
**Post date:** [September 29, 2020, 11:20am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/2 "2020-09-29T11:20:03Z")

</div>

Hey @armygrad24

You could try the below expression to retrieve the “Data4” value

```
$.data.2020-09-29.37677760[:1].data4

```

I have also uploaded a screenshot of the JSON evaluator expression for your reference

 ![jsonData](https://us1.discourse-cdn.com/getpostman/original/3X/6/f/6fe1475719e2611c6967bfd77eb84644f98300a7.png)

Hope this helps you.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 29, 2020, 11:33am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/3 "2020-09-29T11:33:52Z")

</div>

This is a pretty specific path to the data, are you looking for something that is generic or something that finds exactly this piece of data? Meaning, I see the response has today’s date and some random number in there. Plus there is an array of objects.

If you want to get to `data4` with the current response, you can make your code be like this:

```auto
const jsonData = pm.response.json();
const data4 = jsonData.data['2020-09-29'].37677760[0].data4;
pm.environment.set('savedData', data4);

```

I’m assuming you wanted just the integer value of data4 in the savedData variable.

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 29, 2020, 12:04pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/4 "2020-09-29T12:04:53Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> const jsonData = pm.response.json();
> const data4 = jsonData.data['2020-09-29'].37677760[0].data4;
> pm.environment.set('savedData', data4);
> 
> ```

Thanks

But im getting the following error

 ![error](https://us1.discourse-cdn.com/getpostman/original/3X/9/4/94c3a4d0db77f3fc9f019e863dcf3add950623d2.png)

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 29, 2020, 12:09pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/5 "2020-09-29T12:09:30Z")

</div>

[kamalshree2784](https://community.postman.com/u/kamalshree2784)would that be placed in tests?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 29, 2020, 12:19pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/6 "2020-09-29T12:19:45Z")

</div>

Maybe it doesn’t like the numbers as variables. The alternative would be like this:

```auto
const jsonData = pm.response.json();
const data = jsonData.data['2020-09-29']['37677760'][0]['data4'];
pm.environment.set('savedData', data);

```

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 29, 2020, 12:25pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/7 "2020-09-29T12:25:41Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> const jsonData = pm.response.json();
> const data = jsonData.data['2020-09-29']['37677760'][0]['data4'];
> pm.environment.set('savedData', data);
> 
> ```

allen.helton

That worked thank you so much you are the best

Thanks again armygrad

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 30, 2020, 10:57am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/8 "2020-09-30T10:57:13Z")

</div>

is there anyway of using a variable for the date, instead of hard coding the date

Thanks in advance

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 30, 2020, 11:33am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/9 "2020-09-30T11:33:48Z")

</div>

Yes there is.

```auto
let moment = require('moment');
const today = moment().format('YYYY-MM-DD');

const jsonData = pm.response.json();
const data = jsonData.data[today]['37677760'][0]['data4'];
pm.environment.set('savedData', data);

```

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 30, 2020, 11:40am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/10 "2020-09-30T11:40:43Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> let moment = require('moment');
> const today = moment().format('YYYY-MM-DD');
> 
> ```

Again many Thanks, That worked perfectly

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 30, 2020, 11:52am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/11 "2020-09-30T11:52:03Z")

</div>

Happy to help! Let me know if you need anything else!

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 30, 2020, 11:55am UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/12 "2020-09-30T11:55:11Z")

</div>

I will, new to Postman, I Usually use SOAPUI

Again Thanks

---

<div class="post-metadata">

**Author:** ![armygrad24](https://avatars.discourse-cdn.com/v4/letter/a/919ad9/32.png) [@armygrad24](https://community.postman.com/u/armygrad24)\
**Post date:** [September 30, 2020, 1:53pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/13 "2020-09-30T13:53:51Z")

</div>

> [@allenheltondev](#):
>
> ```auto
> const jsonData = pm.response.json();
> const data = jsonData.data[today]['37677760'][0]['data4'];
> pm.environment.set('savedData', data);
> 
> ```

allen

const jsonData = pm.response.json();  
const data = jsonData.data[today][‘37677760’][0][‘data4’];  
pm.environment.set(‘savedData’, data);

how do i set savedData as a global variable?

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [September 30, 2020, 3:26pm UTC](https://community.postman.com/t/retrive-data-from-json-response/16322/14 "2020-09-30T15:26:14Z")

</div>

Instead of doing `pm.environment.set` just use `pm.globals.set`

> **[Using variables | Postman Learning Center](https://learning.postman.com/docs/sending-requests/variables/)**
>
> Using variables: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.
