Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created August 5, 2024 18:35
Show Gist options
  • Save chuckreynolds/883f8a8ec993b2a950432b5c9c6b8ed8 to your computer and use it in GitHub Desktop.
Save chuckreynolds/883f8a8ec993b2a950432b5c9c6b8ed8 to your computer and use it in GitHub Desktop.
Show Top-level field names in console on a Postman API response
// Parse the JSON response body
const responseBody = pm.response.json();
// Check if the response is an array and has elements
if (Array.isArray(responseBody) && responseBody.length > 0) {
// Get the keys (top-level field names) from the first object in the array
const topLevelFields = Object.keys(responseBody[0]);
// Log the top-level field names as a comma-separated string to the Postman console
console.log("Top-level field names in the JSON response: " + topLevelFields.join(', '));
// Optional: Assert that the response contains expected fields
pm.test("Response contains expected fields", function () {
pm.expect(topLevelFields).to.include.members(['name', 'identifier']);
});
} else {
console.log("The response is not a valid array or is empty.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment