Cannot convert XML to Json with xml2Json

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:

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

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.

Hi @dannydainton
Thank you for the reply. I have added the response in the post.

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:

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 :smiley:

1 Like

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

1 Like