0

I have a json response object like this:

"results": [
    {
        "seq": "882818::048313",
        "id": "user1"
    }
]

}

I have the entire json payload and id field name stored in 2 separate variables:

var jsonObj = pm.response.json();
var myfield = "id";

What I would like to do is below:

console.log("Value of id is: " + eval(jsonObj) + eval(".") +      eval(myField));

I tried this way and getting error: Unexpected identifier.

I don't want to hardcode the name of the property but instead make it dynamic.

Please help.

user1893003
  • 45
  • 1
  • 4

1 Answers1

0

If your response body looks like this:

{
    "results": [
        {
            "seq": "882818::048313",
            "id": "user1"
        }
    ]
}

Below statement will work (no need to use eval)

console.log("Value of id is: " +  jsonObj.results[0][myField]);
Anant Naugai
  • 538
  • 4
  • 14