1

I am struggling to extract the data from an array within an array to set as a variable. How would I set the variable from the ID, listed within address

{
"user profile":{
"email_address" : "[email protected]",
"addresses" : [
{
"id": 1
},
{
"id": 2
},
]

For a single array I use

var data = JSON.parse(responseBody); 
postman.setEnvironmentVariable("AddressID",data.user_address[data.addresses.length-1].id);

I'm not quite sure how to use the console log as advised yesterday, if that is the answer to this issue.

Many thanks

asked Aug 4, 2015 at 12:48

1 Answer 1

2
var data = JSON.parse(responseBody); // this will store api response into variable named as data
var len=data.addresses.length; // this is evaluating length of array named as addresses in api response and saving length in variable named as len.
var envvar_akeyvalue=[]; // Here a variable is declared named as envvar_akeyvalue and variable type is defined as array using =[], to begin with it's an empty array
var j=0; //Another variable declared named as j
for (var i=0; i<len; i++) // Here a loop is executed starting from array position 0 until value of len obtained in line 2 of code
 { 
 envvar_akeyvalue[j]=data.addresses[i].id; j=j+1; //this is capturing value of id from addresses array and saving it an array named as envvar_akeyvalue 
 } 
postman. setEnvironmentVariable("id", envvar_akeyvalue); // this is telling postman to set environment variable with name id and obtain it's value(s) from variable envvar_akeyvalue
answered Apr 25, 2016 at 18:17
3
  • May I suggest you add a few extra lines explaining what the code does and how it helps solve the problem? Commented Apr 26, 2016 at 12:29
  • Added comments now @apokryfos Commented Apr 26, 2016 at 20:30
  • Why did I get -2 reputation for my answer? Commented Apr 27, 2016 at 14:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.