P
P
petrov_ruslan2020-07-12 17:18:26
Software testing
petrov_ruslan, 2020-07-12 17:18:26

How to get value from json postman?

{
    "args": {},
    "data": "{\"userId\": 777, \"username\": \"Bob\"}",
    "files": {},
    "form": {},
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-5f0b0dea-dccf35429759ee425a86bd98",
        "content-length": "34",
        "content-type": "text/plain",
        "user-agent": "PostmanRuntime/7.26.1",
        "accept": "*/*",
        "postman-token": "a7fa3df7-4bf3-4c58-a5e8-3604fbdf6dbc",
        "accept-encoding": "gzip, deflate, br",
        "cookie": "sails.sid=s%3AWjeqmvVrZYK5CLY3wWe7XWC6GxGTs_r4.upPw2jg33xMY%2BpNuh5xwTnMCna4YNzj24o48ukBawaA"
    },
    "json": null,
    "url": "https://postman-echo.com/post"
}

I want to get userId, but it doesn't work like this pm.response.json().data.userId, returnsundefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
petrov_ruslan, 2020-07-12
@petrov_ruslan

Did it like this:

var jsonBody = JSON.parse(responseBody);
console.log(jsonBody.data)
var jsonData = JSON.parse(jsonBody.data)
console.log(jsonData.userId)

pm.test("userId from env is equal to userId from response", function () {
    pm.expect(parseInt(pm.environment.get("userId"))).to.equal(
        jsonData.userId
    );
});

If there are other options, I will mark as the answer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question