How many objects in array

Hi!
Could you help me please to verify how many objects in the response?
I tried to verify a particular amount but test was failed.

pm.test("test143", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value).to.eql(200);
});

Is there some opportunity to verify all length?

@NataliaMerkulova

Yes you can get the length using the length().

console.log(jsonData.value.length);

Based on your test above, I beleive you are looking for the length of “value”.

If possible please share the JSON response.

If the response is an array you should try as below:

pm.test(“test143”, function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.value[0]).to.eql(200);
});

Hope this helps!

Hi @bpricilla,
This is a response. The value should be 200, but it’s not. correct, I just need to know the length of the JSON array. Thank you very much!

{
“id”: 65,
“uid”: 8843,
“name”: “SetLibType-I”,
“libType”: “Big Bang!”,
“libCat”: “Zivka Zmurka”,
“records”: ,
“children”: [
{
“id”: 73,
“uid”: 8847,
“name”: “Старик Хоттабыч”,
“records”: ,
“children”:
},
{
“id”: 77,
“uid”: 8918,
“name”: “New Folder”,
“records”:
},
{
“id”: 78,
“uid”: 8844,
“name”: “Bazinga”,
“records”: [
{
“id”: 79,
“uid”: 13651,
“type”: 4,
“criticality”: 700,
“description”: “Changed by test.”,
“audit”: true,
“duration”: 432000
},
{
“id”: 80,
“uid”: 13652,
“type”: 3,
“criticality”: 500,
“description”: “Ручками, ручками.”,
“audit”: false,
“duration”: 604800
}
],
“children”: [
{
“id”: 81,
“uid”: 8845,
“name”: “Sheldon”,
“records”: ,
“children”: [
{
“id”: 82,
“uid”: 8846,
“name”: “Cooper”,
“records”: ,
“children”:
}
]
}
]
},
{
“id”: 88,
“uid”: 8915,
“name”: “Byaka-Buka”,
“records”: ,
“children”:
}
]
},
{
“id”: 90,
“uid”: 761,
“name”: “Pump”,
“libType”: “Pump”,
“libCat”: “Equipment”,
“records”: [
{
“id”: 97,
“uid”: 13656,
“type”: 3,
“criticality”: 500,
“description”: “AAA”,
“audit”: false,
“duration”: 86400
},
{
“id”: 98,
“uid”: 13657,
“type”: 4,
“criticality”: 700,
“description”: “BBB”,
“audit”: false,
“duration”: 172800
},
{
“id”: 99,
“uid”: 13658,
“type”: 3,
“criticality”: 900,
“description”: “CCC”,
“audit”: false,
“duration”: 259200
},
{
“id”: 100,
“uid”: 13659,
“type”: 4,
“criticality”: 100,
“description”: “DDD”,
“audit”: false,
“duration”: 345600
},
{
“id”: 101,
“uid”: 13660,
“type”: 3,
“criticality”: 300,
“description”: “EEE”,
“audit”: false,
“duration”: 432000
},
{
“id”: 102,
“uid”: 13661,
“type”: 4,
“criticality”: 500,
“description”: “FFF”,
“audit”: false,
“duration”: 518400
},
{
“id”: 103,
“uid”: 13662,
“type”: 3,
“criticality”: 500,
“description”: “ZZZ”,
“audit”: false,
“duration”: 604800
},
{
“id”: 104,
“uid”: 13663,
“type”: 3,
“criticality”: 700,
“description”: “Bad schiit happened.”,
“audit”: false,
“duration”: 432000
},
{
“id”: 105,
“uid”: 13664,
“type”: 4,
“criticality”: 300,
“description”: “Копать канаву шириной 5,5 дюймовочек”,
“audit”: true,
“duration”: 1209600
}
],
“children”: [
{
“id”: 106,
“uid”: 776,
“name”: “Spec”,
“records”: ,
“children”:
}
]
},
{
“id”: 109,
“uid”: 762,
“name”: “motor1”,
“libType”: “Big Bang!”,
“libCat”: “Zivka Zmurka”,
“records”: ,
“children”: [
{
“id”: 117,
“uid”: 767,
“name”: “Старик Хоттабыч”,
“records”: ,
“children”:
},
{
“id”: 121,
“uid”: 768,
“name”: “New Folder”,
“records”:
},
{

Hey @NataliaMerkulova, welcome to the community! :wave:

I can’t tell if your response is proper json based on what you pasted above, but in theory what you should do is add the following test:

pm.test('Response has 200 items', function(){
  const jsonData = pm.response.json();
  pm.expect(jsonData.length).to.equal(200);
}

Note - This will work if the response body is an array. I can’t tell if it is or not based on what you’ve shared with us.

Hi @allen.helton, thank you , but it doesn’t work too :frowning:

@allen.helton
This test passed and it should be an array…

const jsonData = pm.response.json();
pm.test(“Test data type of the response”, () => {
pm.expect(jsonData).to.be.an(“array”);

But when I am trying this one:

pm.test(‘Response has 200 items’, function(){
const jsonData = pm.response.json();
pm.expect(jsonData.length).to.equal(200);
}

It doesn’t work…
There was an error in evaluating the test script: SyntaxError: Unexpected end of input

Put a closing parenthesis at the end of the test.

pm.test(‘Response has 200 items’, function(){
  const jsonData = pm.response.json();
  pm.expect(jsonData.length).to.equal(200);
});

@allen.helton
I did it but just didn’t copied

Dear @allen.helton
Thank you, now it is work!!!
Thank you very much!!!
I am happy!

2 Likes