convert xml response to json in python

I’m trying to fetch data from an url in python. It returns xml data.

res = requests.post(url, data=data)
data = json.loads(xmltodict.parse(res.content, attr_prefix=''))
print(data) # shows this error: >>> xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 188

If I do this:

res = requests.post(url, data=data)
print(res.text)

it prints:

<account>
<accountNo   value = "101142325353467512" ></accountNo>
<accountName   value = "Parvej Akter" ></accountName>
<department   value = "5460301" ></department>
<departmentName   value = "S&D-1, Power House, Khulna" ></departmentName>
<sanctionLoad   value = "2" ></sanctionLoad>
</account>

so, how can I convert this response to json? or anyhow get the key and value?

there is a **&** symbol in departmentName tags value. and thats why the error is showing.

to use & in xml, we need to replace to &amp ;

Read more here: Source link