Error in evaluating the test script when retrieving a string from the response in postman

postman test array contains value
postman test script if else
postman typeerror: cannot read property of undefined
postman validate json response
postman test examples
postman response
postman test script write to file
postman test environment variable

From the below response structure i want to retrieve the id field and save it in the environment variable in postman.

I tried all possible methods to do it but unable to do so.

{
    "childMaxAge": 13,
    "selectedForBooking": [
        {
            "id": "0661be61-d0ae-411009-a96d-64229a",
            "firstName": "Test",
            "gender": {
                "code": "MALE",
                "name": "Male"
            }
   ]
}

i have tried the below and it also doesn't work.

tests["Status code is 201"] = responseCode.code === 201;
var data = JSON.parse(responseBody);
console.log(data);
var custid=JSON.parse(data.selectedForBooking[0].id);
tests ["selectedForBooking"]= postman.setEnvironmentVariable("CustId1", custid);

but it gives error :

There was an error in evaluating the test script: JSONError: Unexpected token '6' at 1:2 0661be61-d0ae-411009-a96d-64229a ^

There is no need to parse response data second time. Also there is no need to use tests[] if you're not going to assert anything.

tests["Status code is 201"] = responseCode.code === 201;
var data = JSON.parse(responseBody);
console.log(data);
var custid=data.selectedForBooking[0].id;
postman.setEnvironmentVariable("CustId1", custid);

var custid=JSON.parse(data.selectedForBooking[0].id); was trying to make a json object out of 0661be61-d0ae-411009-a96d-64229a which is not a json formated string.

Saving API response in Postman to a file, How do you get a value from a postman response? As it's a shared entity, I suspect that a team member/the Collection creator may have added a script to a different part of the Collection. There are 3 different levels that a Pre-request Script can be called from in Postman.

Have a look to POSTMAN : parsing a JSON response, how deep can I go into the response?

I used to have a similar problem, I was trying to reach

body.entries[0].attributes.device.type

I managed getting the value by doing

body.entries[0].attributes['device.type']

make sure you use single quotes (I don't know why ...)

Hope this helps

Alexandre

AssertionError on Json array - Help, I have an API Endpoint that returns a JSON array in response: [ { "status": "On I have a test script that asserts for keys using Chaijs expect styling for keys. For some reason the assertion fails with an error because its checking  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Postman test throws error: TypeError: Cannot read property 'ProjectNumber' of undefined

Your JSON data itself is wrong, that is why it was showing that error.

It Must be :

{
    "childMaxAge": 13,
    "selectedForBooking": [
        {
            "id": "0661be61-d0ae-411009-a96d-64229a",
            "firstName": "Test",
            "gender": {
                "code": "MALE",
                "name": "Male"
            }
        }
   ]
}

Now u can try this

var custid=data.selectedForBooking[0].id;

It will return the required value

PostMan gives, I am trying to chain API request on using POSTMAN. I am writing a script in the test section for extracting token from the response body and  Tests will execute after the response is received, so when you click Send, Postman will run your test script when the response data returns from the API. If you need to execute code before a request runs, use Pre-request Scripts instead.

Postman Test Scripting with XML, Find out how MarkLogic lets you test your XML script using API development apps. interactively, testing endpoints with individual requests and getting responses. console.log(xml.xml()); // Serialize the jQuery as a string; var elementContent reading Writing a Behavior-Driven API testing Environment within Postman to  Test examples. Test scripts are run after a request is sent and a response has been received from the server. Let’s look at some examples of Postman tests. Most of these are available as snippets inside Postman. You can run as many tests as you want for a request. Environments Setting an environment variable

Collection Runner stops running with error "Cannot read property , App Details: Postman for Windows Version 5.5.0 win32 10.0.16299 / ia32 Issue Report: I have Now I set empty string and the project runs without errors. My test scripts have " pm.globals.set("newuser_id", data.data.id)" ,when the const response = pm.response.json(); pm.globals.set("createdStoreId",  Open Pre-request Scripts to enter code that will run before every request in the collection or folder. You can define a pre-request script when you first create a collection or folder, or at any time after that. Next steps. For more detail on what you can do in your pre-request scripts, check out Test Scripts and the Postman Sandbox.

Error during JSON.parse Unexpected token u in JSON at position 0 , 200 Want to notice that we receive JSON format response (I asked Hi @​Alextunchii - I think you're trying to parse undefined or something that's a string maybe ? console.log(responseBody.message) as a test script in the request Also, are you on the latest version of the Postman app - Postman 6 ? Writing tests inside this function allows you to name the test accurately and this function also ensures the rest of the script is not blocked even if there are errors inside the function. The following sample test checks that everything about a response is valid to proceed.

Comments
  • Hi, i tried what you suggested but that also giving error "There was an error in evaluating the test script: TypeError: Cannot read property 'id' of undefined"
  • your error indicates that array under "selectedForBooking" is empty, have you checked that response is actually returned? Is your service publicly available? Can I fire request into it?
  • Hi, i tried what you suggested but that also giving error "There was an error in evaluating the test script: TypeError: Cannot read property 'id' of undefined"
  • well, it's not exaclty the same case as mine, have you tried to console.log(custid) before the test ? with this error, it looks like selectedForBooking[0] is not recognized ... could you also have a look, in the console, to selectedForBooking[0] ?
  • ?? why do you json.parse data, which is already the result of a json parsing ?