Created
August 5, 2024 18:35
-
-
Save chuckreynolds/883f8a8ec993b2a950432b5c9c6b8ed8 to your computer and use it in GitHub Desktop.
Show Top-level field names in console on a Postman API response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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