# Cannot convert XML to Json with xml2Json

**URL:** <https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105>\
**Category:** Help Hub\
**Created:** [September 22, 2020, 7:25am UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105 "2020-09-22T07:25:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![miis](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@miis](https://community.postman.com/u/miis)\
**Post date:** [September 22, 2020, 7:25am UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105/1 "2020-09-22T07:25:59Z")

</div>

I am trying to get the number of entries in an XML response by converting it to Json.

The response has this content type:  
`Content-Type: application/xml; charset=utf-8`

But running this in Test does not give the expected output:  
var jsonObject = xml2Json(responseBody);  
console.log(typeof jsonObject); – object. How do I get a Json object?  
console.log(jsonObject.length); – undefined

Output:  
“object”  
undefined

How can I fix this so I can count entries in the response?

Response example:

> ```
> <?xml version="1.0" encoding="utf-16"?>
> <CIM xmlns:DT="CIM_DataTypes">
> <party>
> <rank>D</rank>
> <partyId>2348703043</partyId>
> </party>
> <party>
> <rank>E</rank>
> <partyId>2334403020</partyId>
> </party>
> </CIM>
> 
> ```

console.log(jsonObject);  
outputs something like this:

```auto
{CIM: {…}}
CIM: {…}
$: {…}
xmlns:DT: "CIM_DataTypes"
Party: [399]
0: {…}
rank: "D"
partyId: "2348703043"
1: {…}
2: {…}
.
.
.

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 22, 2020, 7:38am UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105/2 "2020-09-22T07:38:19Z")

</div>

Hey @miis

Can you provide an example of what the XML response is please.

It’s hard to tell what you’re trying to get the length of without that information.

---

<div class="post-metadata">

**Author:** ![miis](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@miis](https://community.postman.com/u/miis)\
**Post date:** [September 22, 2020, 9:07am UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105/3 "2020-09-22T09:07:34Z")

</div>

Hi @danny-dainton  
Thank you for the reply. I have added the response in the post.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [September 22, 2020, 9:32am UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105/4 "2020-09-22T09:32:05Z")

</div>

I’m not sure that I fully understand what you’re trying to count here - It looks like the `party` array could have X number of items so you could get that number by using:

```auto
console.log(jsonObject.CIM.party.length)

```

If that’s not the data point that you need, would you be able to expand on that a bit more so I can offer a more targeted solution 😃

---

<div class="post-metadata">

**Author:** ![miis](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@miis](https://community.postman.com/u/miis)\
**Post date:** [September 22, 2020, 1:03pm UTC](https://community.postman.com/t/cannot-convert-xml-to-json-with-xml2json/16105/5 "2020-09-22T13:03:21Z")

</div>

This is exactly what I wanted to do.  
Thank you so much, now I can test the number of entries.
