How to check if exists property in json?

Hi @pablovok, just wanted to weigh in here about using lodash which is inbuilt in Postman and how you can easily get around this code piece.

Using the _.get method from lodash, which allows safe extraction you’ll be able to avoid try...catch blocks.

pm.test('Validate date', function () {
  var idDate = _.get(jsonData, 'date[0].id');

  if (idDate) {
    pm.expect(jsonData.date[0]).to.have.property('id');
  } else {
    pm.expect(jsonData.date).to.have.property('id');
    idDate = _.get(jsonData, 'date.id');
  }

  console.log(idDate);
  pm.environment.set('IdDATE - TEMP', idDate);
});
8 Likes