armygrad24
(Marco Zani)
September 29, 2020, 10:12am
1
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
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
Hope this helps you.
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:
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.
armygrad24
(Marco Zani)
September 29, 2020, 12:04pm
4
Thanks
But im getting the following error
armygrad24
(Marco Zani)
September 29, 2020, 12:09pm
5
kamalshree2784 would that be placed in tests?
Maybe it doesn’t like the numbers as variables. The alternative would be like this:
const jsonData = pm.response.json();
const data = jsonData.data['2020-09-29']['37677760'][0]['data4'];
pm.environment.set('savedData', data);
armygrad24
(Marco Zani)
September 29, 2020, 12:25pm
7
allen.helton
That worked thank you so much you are the best
Thanks again armygrad
1 Like
armygrad24
(Marco Zani)
September 30, 2020, 10:57am
8
is there anyway of using a variable for the date, instead of hard coding the date
Thanks in advance
Yes there is.
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);
armygrad24
(Marco Zani)
September 30, 2020, 11:40am
10
Again many Thanks, That worked perfectly
1 Like
Happy to help! Let me know if you need anything else!
armygrad24
(Marco Zani)
September 30, 2020, 11:55am
12
I will, new to Postman, I Usually use SOAPUI
Again Thanks
armygrad24
(Marco Zani)
September 30, 2020, 1:53pm
13
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?
Instead of doing pm.environment.set
just use pm.globals.set