How to check length key of JSON response

I have JSON data response as below:

{
    "response": {
        "numFound": 0,
        "start": 0,
        "maxScore": 0,
        "docs": []
    }
}

Now, i want check the length of numfound, I had used the script as below but not working:

var jsonData = JSON.parse(responseBody);
tests["Check length of  numfound"] = Object.keys(jsonData.response.numFound).length === 1

Hey @thaichau298,

As that value is a number or an integer, you wouldn’t be able to use the .length method on that. That would be something that you could use if the type was a string or an array.

More information on that can be found here:

https://www.w3schools.com/jsref/jsref_length_string.asp

https://www.w3schools.com/jsref/jsref_length_array.asp

You would be able to check the value of that number using something like this if you wanted to check for a particular value.

pm.test('Value check', () => {
    let jsonData = pm.response.json()
    pm.expect(jsonData.response.numFound).to.equal(1)
})

This is just a way of writing a test using the newer pm.* API syntax.

1 Like