Response body PM test

Hi, can someone help me out with this issue
I’m looking for a PM test: which pull json value from response body
The response is in JSON

[{
“id”: “2205440117001”,
“account_id”: “936125974001”,
“ad_keys”: null,
“clip_source_video_id”: null,
“complete”: true,
“created_at”: “”,
“created_by”: {
“type”: “unknown”
},
“cue_points”: ,
“custom_fields”: {},
“delivery_type”: “dynamic_origin”,
“description”: “033_mots_k_1_02-4”,
“digital_master_id”: null,
“duration”: 252842,
“economics”: “AD_SUPPORTED”,
“folder_id”: “62c7dee1ecf84a72a5e7c502”,
“geo”: null,
“has_digital_master”: true,
“images”: {
“thumbnail”: {
“src”: “”,
“sources”: [{
“src”: “”,
“height”: 90,
“width”: 160
}]
},
“poster”: {
“src”: “”,
“sources”: [{
“src”: “”,
“height”: 480,
“width”: 640
}]
}
},
“link”: null,
“long_description”: null,
“name”: “mots_k_1_02-4”,
“original_filename”: “En_033.mp4”,
“projection”: null,
“published_at”: “2013-03-05T22:12:46.099Z”,
“reference_id”: “En_033”,
“schedule”: null,
“sharing”: null,
“state”: “ACTIVE”,
“tags”: [“mots”],
“text_tracks”: ,
“transcripts”: ,
“updated_at”: “Z”,
“updated_by”: {
“type”: “internal”
},
“playback_rights_id”: null
}]

Hi @postmall_desk

Which value are you trying to get from that response?

Hi @w4dd325 ,
thanks for getting back so quickly,
these are the values I need

{

“id”: “{{id}}”,

“account_id”: “{{account_id}}”,

“reference_id”: “{{reference_id}}”,

“name”: “{{name}}”

}

Try this;

const response = pm.response.json();

console.log('id = ' + response[0].id);
pm.collectionVariables.set("id", response[0].id);

console.log('account_id = ' + response[0].account_id);
pm.collectionVariables.set("account_id", response[0].account_id);

console.log('reference_id = ' + response[0].reference_id);
pm.collectionVariables.set("reference_id", response[0].reference_id);

console.log('name = ' + response[0].name);
pm.collectionVariables.set("name", response[0].name);

Note; Uses **response[0]** as the entire response is stored in an array (starts with **[** and ends with **]** … hopefully that makes sense).

Output;

Collection level variables saved;

Thanks for your quick reply it worked, but every time I try to copy the response from the consol.log, it only copies a few lines.is the a way of exporting the response from the console.log?

Thanks for your quick reply, it works. But every time I try to copy the response from the console.log,it only copies a few lines> Is there a way of exporting the body response from the console.log?
Thanks
Zara

Do you want to actually test against the values, or just save the values to use in a later call. You can set them in your environment or your collection variables like w4dd325 suggested, or use the snippest on the right pane of the test tab to generate a test template for you.
image

There is also this option,
image

You have this code in my example above;

const response = pm.response.json();

if you console.log it;

console.log(response);

then you would be able to copy it to the clipboard.

Thank you this worked. I have also started using Newnam as a library to run my collections. I’m getting an error
[{“error_code”: “UNAUTHORIZED”, “message”: “Permission denied.”}]. I downloaded my collection and environment.

newman.run({
collection: require(‘./test_collections_scripts.postman_collection.json’),
environment: ‘./collection_env.postman_environment.json’,
iterationData: ‘./test_collections_scripts.postman_collection.json’',
reporters: ‘cli’,
}
The iteration are running here is the response