0

I have a response the array with objects like this:

[
    {
        "id": 1,
        "name": "project one"
    },
    {
        "id": 2,
        "name": "project two"
    },
    {
        "id": 3,
        "name": "project three"
    }
]

Have can I check if my responsed array have an object { "id": 3, "name": "project three" } for example? I am trying to check by this way but it didn't work:

pm.test('The array have object', () => {
    pm.expect(jsonData).to.include(myObject)
})

3 Answers 3

2

you can validate this also using includes after converting it to string using JSON.stringify

pm.expect(JSON.stringify(data)).to.include(JSON.stringify({
    "id": 3,
    "name": "project three"
}))

Also you can use lodash function some/any:

pm.expect(_.some(data,{
    "id": 3,
    "name": "project three"
})).to.be.true

https://lodash.com/docs/3.10.1#some

Note: Postman works in a sandbox and only below libraries are suported:

https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-external-libraries

Sign up to request clarification or add additional context in comments.

1 Comment

If you need a higher version of lodash than the provided global _ object (which at the time of writing uses 3.10.1) you must require it manually: var latestLodashVersion = require('lodash');
1

pm.expect(jsonData).to.include(myObject) works for String but not for Object. You should use one of the following functions and compare each property of the object:

  • Array.filter()
  • Array.find()
  • Array.some()

Examples:

data = [
    {
        "id": 1,
        "name": "project one"
    },
    {
        "id": 2,
        "name": "project two"
    },
    {
        "id": 3,
        "name": "project three"
    }
];
let object_to_find = { id: 3, name: 'project three' }

// Returns the first match
let result1 = data.find(function (value) {
    return value.id == object_to_find.id && value.name == object_to_find.name;
});

// Get filtered array
let result2 = data.filter(function (value) {
    return value.id == object_to_find.id && value.name == object_to_find.name;
});

// Returns true if some values pass the test
let result3 = data.some(function (value) {
    return value.id == object_to_find.id && value.name == object_to_find.name;
});

console.log("result1: " + result1.id + ", " + result1.name);
console.log("result2 size: " + result2.length);
console.log("result3: " + result3);

Use one of the ways while asserting in Postman.

Comments

1

You can also use the to.deep.include syntax. In your case, it would look like this:

pm.test('The array have object', () => {
    pm.expect(jsonData).to.deep.include({ id: 2, name: "project two" })
    pm.expect(jsonData).to.deep.include({ id: 3, name: "project three" })
    pm.expect(jsonData).to.deep.include({ id: 1, name: "project one" })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.