Size of JSON array


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


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

Can anyone help? Thanks!!!

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

I do not know how to post xml here…


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

 x = [

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

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

Is that what you were trying to achieve?

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

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.

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

Take a look at this javascript I wrote to test.

I think you’re just missing JSON.parse


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

This is now working:

var jsonObject = xml2Json(responseBody);

let hasMemberArray = jsonObject[‘PizzaMessage’][‘hasMember’]
pm.test(“response contains exactly three hasMember entries”, function () {

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

1 Like