-1

I am making a rest call to a webservice. In its body it contains a Json file with 18 pairs key, value.

I which to make separate operations on each element so i would like to:

  • get the different key elements. so i would have an array of the key elements.

  • With each of those elements, get the corresponding value by dot walking.

Here is what I have:

var obj = JSON.parse(request.body.dataString);

var myKeys = Object.keys(obj)

for (var i = 0; i < myKeys.length; i++){

   var iter = myKeys[i];

   gs.log(obj.iter);
}

But the gs.log returns undefined.

I have tried to make this:

gs.log(obj.myId);

and it returns the value i want, since myId is one of the elements in keys.

How can i use the elements of my keys array to get the corresponding values?

I have confirmed that hte var myKeys has all the elements of keys and the var obj contains all the json file.

  • http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json?answertab=active#tab-top –  May 12 '17 at 13:33
  • What is `gs.log`? What is `.iter`? What do you expect? Can you show some sample JSON? – evolutionxbox May 12 '17 at 13:34

1 Answers1

1

You need to use the [] syntax.

var iter = myKeys[i];

gs.log(obj[iter]);

Plain obj.iter tries to get a property called literally iter. If you want to get a property based on the string value of the variable iter, you need to access the object using obj[iter].

Karl Reid
  • 2,147
  • 1
  • 10
  • 16