# Size of JSON array

**URL:** <https://community.postman.com/t/size-of-json-array/7071>\
**Category:** Help Hub\
**Tags:** json, xml-response\
**Created:** [August 9, 2019, 11:44am UTC](https://community.postman.com/t/size-of-json-array/7071 "2019-08-09T11:44:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 11:44am UTC](https://community.postman.com/t/size-of-json-array/7071/1 "2019-08-09T11:44:47Z")

</div>

Hi.

I have calls that return me an xml. To navigate the response, I first convert it to JSON with the following:

var jsonObject = xml2Json(responseBody);

I then address its parts with variables hierarchically like this:

var x = jsonObject[‘x’]  
var y = x[‘y’]

My goal is to use the test script to figure out how many elements the x-array has, i.e. how many y-blobs there are (how many y children of the main x object).

I tried with

x.length

and the previous definition of x, but in this case I get null.

Can anyone help? Thanks!!!

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 9, 2019, 12:12pm UTC](https://community.postman.com/t/size-of-json-array/7071/2 "2019-08-09T12:12:20Z")

</div>

Would you mind providing an example of what your xml/json looks like please?

---

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 12:22pm UTC](https://community.postman.com/t/size-of-json-array/7071/3 "2019-08-09T12:22:31Z")

</div>

I do not know how to post xml here…

---

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 12:22pm UTC](https://community.postman.com/t/size-of-json-array/7071/4 "2019-08-09T12:22:54Z")

</div>

![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/6/86c54794c33c5f262048fe6dd2d7028ddd122d56.png)

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 9, 2019, 12:25pm UTC](https://community.postman.com/t/size-of-json-array/7071/5 "2019-08-09T12:25:43Z")

</div>

Looking at the XML picture you provided,  
you’ll be getting a JSON as:

```auto
 x = [
  "y1",
  "y2"
]

```

If you just write `x.length`, you’ll actually get the number of items there are there in the `x` array.

```javascript
var x = jsonObject[‘x’],
  numberOfYItems = x.length;

```

Is that what you were trying to achieve?

---

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 12:27pm UTC](https://community.postman.com/t/size-of-json-array/7071/6 "2019-08-09T12:27:35Z")

</div>

It is, but I get null. I will try to provide a more realistic example (I have to change label names and so on).

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 9, 2019, 12:30pm UTC](https://community.postman.com/t/size-of-json-array/7071/7 "2019-08-09T12:30:34Z")

</div>

Ohh, there could be some other issue.  
`Array.length` can never give you null, if it’s not an array but an object then it will give you an `undefined`.

I think the `x` is actually an object here which already has a property `length` with a value `null`

Can you write `console.log(x, Array.isArray(x));` and open Postman console and show the result after you send the request?  
Feel free to retract any sensitive information.

---

<div class="post-metadata">

**Author:** ![allenheltondev](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/allenheltondev/32/31738_2.png) [@allenheltondev](https://community.postman.com/u/allenheltondev)\
**Post date:** [August 9, 2019, 12:31pm UTC](https://community.postman.com/t/size-of-json-array/7071/8 "2019-08-09T12:31:59Z")

</div>

The command you are using to convert to json is just converting the xml to a json string.

Take a look at [this javascript](https://repl.it/repls/AmusedFarflungMysql) I wrote to test.

I think you’re just missing JSON.parse

---

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 1:13pm UTC](https://community.postman.com/t/size-of-json-array/7071/9 "2019-08-09T13:13:11Z")

</div>

Ok, the trouble seems to come from the XML, which is a bit strange. I will have to investigate it. Your example works fine.

---

<div class="post-metadata">

**Author:** ![giuseppe.lacagnina](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/giuseppe.lacagnina/32/1689_2.png) [@giuseppe.lacagnina](https://community.postman.com/u/giuseppe.lacagnina)\
**Post date:** [August 9, 2019, 1:27pm UTC](https://community.postman.com/t/size-of-json-array/7071/10 "2019-08-09T13:27:07Z")

</div>

This is now working:

var jsonObject = xml2Json(responseBody);

let hasMemberArray = jsonObject[‘PizzaMessage’][‘hasMember’]  
pm.test(“response contains exactly three hasMember entries”, function () {  
pm.expect(hasMemberArray.length).to.equal(3);  
});

I was probably doung something really stupid. Thanks, everyone!!!
