im having this byte class
b'ToCountry=US&ToState=WA&SmsMessageSid=SM2c04173b9a5f684be8019e177978c791&NumMedia=0&ToCity=&FromZip=&SmsSid=SM2c04173b9a5f684be8019e177978c791&FromState=&SmsStatus=received&FromCity=&Body=Bbjhggggggg&FromCountry=EE&To=+15095121752&ToZip=&NumSegments=1&ReferralNumMedia=0&MessageSid=SM2c04173b9a5f684be8019e177978c791&AccountSid=ACee01b40141d9e1237769375c269f4a76&From=+37253055607&ApiVersion=2010-04-01'
how can i convert to json
CodePudding user response:
One line solution
a = b'ToCountry=US&ToState=WA&SmsMessageSid=SM2c04173b9a5f684be8019e177978c791&NumMedia=0&ToCity=&FromZip=&SmsSid=SM2c04173b9a5f684be8019e177978c791&FromState=&SmsStatus=received&FromCity=&Body=Bbjhggggggg&FromCountry=EE&To=+15095121752&ToZip=&NumSegments=1&ReferralNumMedia=0&MessageSid=SM2c04173b9a5f684be8019e177978c791&AccountSid=ACee01b40141d9e1237769375c269f4a76&From=+37253055607&ApiVersion=2010-04-01'
dict = {e.split("=")[0]:e.split("=")[1] for e in a.decode().split("&")}
{'ToCountry': 'US', 'ToState': 'WA', 'SmsMessageSid': 'SM2c04173b9a5f684be8019e177978c791', 'NumMedia': '0', 'ToCity': '', 'FromZip': '', 'SmsSid': 'SM2c04173b9a5f684be8019e177978c791', 'FromState': '', 'SmsStatus': 'received', 'FromCity': '', 'Body': 'Bbjhggggggg', 'FromCountry': 'EE', 'To': '+15095121752', 'ToZip': '', 'NumSegments': '1', 'ReferralNumMedia': '0', 'MessageSid': 'SM2c04173b9a5f684be8019e177978c791', 'AccountSid': 'ACee01b40141d9e1237769375c269f4a76', 'From': '+37253055607', 'ApiVersion': '2010-04-01'}
CodePudding user response:
Use the standard module urllib.parse
, which will also take care about %
-encoded symbols:
from urllib.parse import parse_qsl
url = 'ToCountry=US&ToState=WA&To=+15095121752' # ...etc
result = dict(parse_qsl(url))