0

I am trying to parse the json response from post request and send the parsed data to put request this is my response body

    {
        "createdBy": "student",
        "createdOn": "2019-06-18",
        "Id1": "0e8b9445-4bd9-4d31",
        "Tl": [
            {
                "createdBy": "student",
                "createdOn": "2019-06-18",
                "Id2": "d46eeb88-f876-4468"
            }
        ]
    }   

I am parsing id1 and id2 which are auto generated. This is the code I am writing in tests

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Id1", jsonData.Id1);
postman.setEnvironmentVariable("Id2", jsonData.Tl[2].Id2);

id1 is working but I am unable to access Id2 and getting the error after post as

typeerror cannot read Id2 property

and I am accessing Id2 in put request as {{Id2}}

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
twittyph
  • 1
  • 1
  • 1
  • 3
    `jsonData.Tl` has only one element so you should use `jsonData.Tl[0].Id2` – barbsan Jun 18 '19 at 09:41
  • 3
    Does this answer your question? [Checking a value in a nested JSON using Postman](https://stackoverflow.com/questions/42850233/checking-a-value-in-a-nested-json-using-postman) – Henke Jan 24 '21 at 08:36

2 Answers2

1

In the following statement, you are trying to access the 3rd element (index: 2) of the array with key 'Tl' :

postman.setEnvironmentVariable("Id2", jsonData.Tl[2].Id2);

You should access the 1st element only (index:0) :

postman.setEnvironmentVariable("Id2", jsonData.Tl[0].Id2);
AJain
  • 11
  • 1
0

There's only one element in jsonData.Tl - so jsonData.Tl[2] is undefined. Use jsonData.Tl[0] to access the first element.

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Id1", jsonData.Id1);
postman.setEnvironmentVariable("Id2", jsonData.Tl[2].Id2);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79