In the past I would look in the json object and find the object and save it, but I’m not sure how to do so with a string. EG If i try something like this
Then session_id just comes back as undefined. Is it because It’s not an object in the JSON?
I know it can see the string. If i do a simple test like so
pm.test("Body matches string", function () {
pm.expect(pm.response.text()).to.include("session_id");
The shared JSON data looks like a string, so try converting to JSON object, and that’s probably the reason you are getting session_id as undefined.
var RandData = JSON.parse("{\"a_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 16.59], \"b_gains\": [1.8499999999999996, 20.15, 19.7, 13.275, 9.4, 12.5, 32.59], \"session_id\": 1795, \"sound_file\": \"gs://sound-files-test/comparison/8k/comparison_Listen_to_the.wav\", \"step_count\": 1}");
/*
var data = pm.response.json()
*/
/* you can set the collection/environment/global variables (below is collectionVariables)*/
pm.collectionVariables.set("session_id", RandData.session_id)
/*To check whether you have session_id as key or not*/
pm.test("session_id is available in JSON data", function () {
pm.expect(RandData).to.have.property('session_id');
});
And if you are not converting to JSON object and directly checking for session_id.
pm.test("Body matches string", function () {
pm.expect(RandData).to.include("session_id");
});
This worked thank you. The issue is that i’m having is session_id will always return a different number, so in my example it was 1795 but it changes everytime. Is there a way to parse the JSON so it returns that value instead of a hard coded one?
Edit: I’ve found the solution but thank you for pointing me in the right direction.
let response = pm.response.json();
var RandData = JSON.parse(response);
postman.setEnvironmentVariable("session_id", RandData.session_id);