How can i Assert that there are 3 fixtures within the returned JSON object

[
    {
        "fixtureId": "1",
        "fixtureStatus": {
            "displayed": false,
            "suspended": true
        },
        "footballFullState": {
            "homeTeam": "Dagenham & Redbridge",
            "awayTeam": "Ă–sterreich",
            "finished": false,
            "gameTimeInSeconds": 2656,
            "goals": [
                {
                    "clockTime": 640,
                    "confirmed": true,
                    "id": 678606,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 560617,
                    "teamId": "1"
                },
                {
                    "clockTime": 864,
                    "confirmed": true,
                    "id": 164002,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 60817,
                    "teamId": "2"
                },
                {
                    "clockTime": 1312,
                    "confirmed": true,
                    "id": 267245,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 136629,
                    "teamId": "1"
                },
                {
                    "clockTime": 1760,
                    "confirmed": true,
                    "id": 758030,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 131840,
                    "teamId": "2"
                }
            ],
            "period": "SECOND_HALF",
            "possibles": [],
            "corners": [],
            "redCards": [],
            "yellowCards": [],
            "startDateTime": "2018-03-20T10:49:38.655Z",
            "started": true,
            "teams": [
                {
                    "association": "HOME",
                    "name": "Dagenham-&-Redbridge",
                    "teamId": "HOME"
                },
                {
                    "association": "AWAY",
                    "name": "Ă–sterreich",
                    "teamId": "AWAY"
                }
            ]
        }
    },
    {
        "fixtureId": "2",
        "fixtureStatus": {
            "displayed": true,
            "suspended": false
        },
        "footballFullState": {
            "homeTeam": "Manchester United",
            "awayTeam": "Leeds United",
            "finished": false,
            "gameTimeInSeconds": 900,
            "goals": [
                {
                    "clockTime": 640,
                    "confirmed": true,
                    "id": 678606,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 560617,
                    "teamId": "1"
                },
                {
                    "clockTime": 864,
                    "confirmed": true,
                    "id": 164002,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 60817,
                    "teamId": "2"
                }
            ],
            "period": "FIRST_HALF",
            "possibles": [],
            "corners": [],
            "redCards": [],
            "yellowCards": [],
            "startDateTime": "2018-03-20T10:49:38.655Z",
            "started": true,
            "teams": [
                {
                    "association": "HOME",
                    "name": "Manchester-United",
                    "teamId": "HOME"
                },
                {
                    "association": "AWAY",
                    "name": "Leeds-United",
                    "teamId": "AWAY"
                }
            ]
        }
    },
    {
        "fixtureId": "3",
        "fixtureStatus": {
            "displayed": false,
            "suspended": true
        },
        "footballFullState": {
            "homeTeam": "Garforth FC",
            "awayTeam": "York FC",
            "finished": false,
            "gameTimeInSeconds": 950,
            "goals": [
                {
                    "clockTime": 640,
                    "confirmed": true,
                    "id": 678606,
                    "ownGoal": false,
                    "penalty": false,
                    "period": "FIRST_HALF",
                    "playerId": 560617,
                    "teamId": "1"
                }
            ],
            "period": "FIRST_HALF",
            "possibles": [],
            "corners": [],
            "redCards": [],
            "yellowCards": [],
            "startDateTime": "2018-03-20T10:49:38.655Z",
            "started": true,
            "teams": [
                {
                    "association": "HOME",
                    "name": "Garforth-FC",
                    "teamId": "HOME"
                },
                {
                    "association": "AWAY",
                    "name": "York-FC",
                    "teamId": "AWAY"
                }
            ]
        }
    },
   ]

Hey @seyous,

It looks like that’s an array of 3 objects, I’m guessing that’s the 3 fixtures you want to test for?

Something simple like this could do that:

pm.test("Should be 3 fixtures", () => {
    pm.expect(pm.response.json().length).to.eql(3)
}) 

More information about working with JSON responses can be found here:

1 Like

Thanks @dannydaiton you are a genius.

How can I Assert that each of the 3 fixtures has a fixtureId value?

I would suggest taking a look through the link I posted and trying to figure that out.

If I, or others on here just give you a single answers each time, there isn’t any real learning on.

If you give it a go and you end up getting stuck, post your code on here and I will be happy to take a look and give you some pointers.

1 Like

I tried something like this:

 json_response = JSON.parse(responseBody)
 x = json_response.fixtureId
 pm.expect(x).to.equal(1)

I get this error:

AssertionError: expected undefined to equal 1

Awesome :trophy: - So at least you gave it a go, which is :+1:

You were on the right track for checking the first fixtureId value, just needed a slight change:

pm.test("Check the fixtureId value", () => {
    // You needed to add the [0] at the end to get the first object
    let json_response = JSON.parse(responseBody)[0]
    let x = json_response.fixtureId
    // The value is a string and not an integer so you needed to wrap the number in quotes
    pm.expect(x).to.equal("1")
}) 

To answer your question:

How can I Assert that each of the 3 fixtures has a fixtureId value?

As the objects (**{..}**) are wrapped in an array(**[]**) you would need to loop through them, I’m using the lodash .each() function but you could use the native JS .forEach() function to do the same thing. It’s setting each of the objects as arrItem so that you can then access each of the properties within the object.

To do the check to see if that key is in each of the objects, I’m using .to.have.own.property('key_name') from the Chaijs in the pm.expect() function and to check if the value is a string, I’m using .to.be.a('string').

pm.test("The fixtureId property is in the object and is a string", () => {
    _.each(pm.response.json(), (arrItem) => {
        // The property is in the object
        pm.expect(arrItem).to.have.own.property('fixtureId')
        // The value is a string
        pm.expect(arrItem.fixtureId).to.be.a('string')
        
        // If you wanted to do some additional checks
        pm.expect(arrItem.fixtureId).to.not.be.null
        pm.expect(arrItem.fixtureId).to.not.be.undefined
        pm.expect(arrItem.fixtureId).to.not.be.empty
    })
})

Hope that helps you out :slight_smile:

You are great Man.:+1:

1 Like