Home > Software design >  Convert JSON response to a dictionary
Convert JSON response to a dictionary

Time:08-12

I got the following response from an a request (respone below is sliced). However, when I try to convert the response to a dictionary it throws back an error. What is wrong with "response"?

response= b'\xef\xbb\xbf<message:MessageGroup xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic http://www.sdmx.org/docs/2_0/SDMXGenericData.xsd http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message ht'

json.loads(str(response))

enter image description here

CodePudding user response:

you have to decode your response

try this:

import codecs

decoded_data=codecs.decode(response, 'utf-8-sig')

print(decoded_data)

you can read the article : https://www.howtosolutions.net/2019/04/python-fixing-unexpected-utf-8-bom-error-when-loading-json-data/

hope this will help

CodePudding user response:

This solved it.

import xmltodict

response= b'\xef\xbb\xbf<message:MessageGroup xmlns="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic" xmlns:common="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/common" xsi:schemaLocation="http://www.SDMX.org/resources/SDMXML/schemas/v2_0/generic http://www.sdmx.org/docs/2_0/SDMXGenericData.xsd http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message ht'

xmltodict.parse(response)
  • Related