How can I validate that my response json does not contain the name object?
without knowing where the name object appears in the json
Response:
{
data: “hello”,
zone: “EEUU”,
city {
name: “california”
}
}
How can I validate that my response json does not contain the name object?
without knowing where the name object appears in the json
Response:
{
data: “hello”,
zone: “EEUU”,
city {
name: “california”
}
}
Hi pablovok,
Can you write a test on the Tests tab to look for the presence of that element?
pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
pm.expect(jsonData.city).to.have.property(“name”);
});
Hi Jhon,
Following his example, how could I validate it if I don’t know what position the city is in? It may be in jsonData.city or jsonData.country.city …
One way that occurred to me is to pass the json to text and validate that the camp does not exist.
Thanks and regards.
You can collect all the keys and check for the presence/absence of the ‘name’ as a key. I would hope you could narrow the scope and only check the elements where name might be a child, city and country in case name is used elsewhere.
pm.test(“Your test name”, function () {
var jsonData = pm.response.json();
Object.keys(jsonData).forEach(function(key) {
if (key===“name”){
console.log(‘fail’)
}
});
});
Thanks for your reply and time.
Try what I recommend, but just bring the first key, and not the daughters.