Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) #64

Closed
dacrypt opened this issue Dec 26, 2019 · 3 comments

Comments

@dacrypt
Copy link
Contributor

dacrypt commented Dec 26, 2019

https://aic.lgthinq.com:46030/api/rti/rtiControl {'cmd': 'Config', 'cmdOpt': 'Get', 'value': 'DuctZone', 'deviceId': 'd27c7740-7149-11d3-80b4-7440be9deeb2', 'workId': 'a3dcce9c-af3d-1024-a8a0-cfbb0c395bb3', 'data': ''} <Response [200]> {'returnCd': '0000', 'returnMsg': 'OK', 'deviceId': 'd27c7740-7149-11d3-80b4-7440be9deeb2', 'stateCode': 'S', 'workId': 'n-a3dcce9c-af3d-1024-a8a0-cfbb0c395b', 'returnData': 'e1t7Ik5vIjoiMSIsIkNmZyI6IjEiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiMiIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiMyIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiNCIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiNSIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiNiIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiNyIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifSx7Ik5vIjoiOCIsIkNmZyI6IjAiLCJTdGF0ZSI6IjAifV19', 'format': 'B64', 'deviceState': 'E', 'timestamp': 0}

Traceback (most recent call last):
File "example.py", line 230, in
main()
File "example.py", line 226, in main
example(args.country, args.language, args.cmd, args.args)
File "example.py", line 186, in example
example_command(client, cmd, args)
File "example.py", line 162, in example_command
func(client, *args)
File "example.py", line 147, in ac_config
print(ac.get_zones())
File "/Users/david/dev/unsorted/wideq/wideq/ac.py", line 196, in get_zones
return self._get_config('DuctZone')
File "/Users/david/dev/unsorted/wideq/wideq/client.py", line 440, in _get_config
return json.loads(base64.b64decode(data).decode('utf8'))
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/init.py", line 348, in loads
return _default_decoder.decode(s)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/decoder.py", line 353, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

@sampsyo
Copy link
Owner

sampsyo commented Dec 26, 2019

Weird; it looks like that status string decodes to:

{[{"No":"1","Cfg":"1","State":"0"},{"No":"2","Cfg":"0","State":"0"},{"No":"3","Cfg":"0","State":"0"},{"No":"4","Cfg":"0","State":"0"},{"No":"5","Cfg":"0","State":"0"},{"No":"6","Cfg":"0","State":"0"},{"No":"7","Cfg":"0","State":"0"},{"No":"8","Cfg":"0","State":"0"}]}

…which of course is not valid JSON. You can't start a JSON document with {[. It looks like they took a proper JSON list and wrapped it in {curly braces} for no reason?

I guess we could detect the error, try just stripping off the { and }, and try again. Or we could pass a special flag just for the "zones" configuration that does that.

@dacrypt
Copy link
Contributor Author

dacrypt commented Jan 22, 2020

Since we have determined that LG can return a json wrapped in curly braces, I think detecting the error is the best long term solution. You never know when other json results might come like.

@beele
Copy link

beele commented May 30, 2020

This is easily fixed by checking for such malformed json and than apply a fix at both start and end:

# Added fix to correct malformed JSON!
return json.loads(base64.b64decode(data).decode('utf8').replace('{[', '[').replace(']}', ']'))

in client.pi#435
To clean this up one could create a method to sanitize the raw json string before trying to json parse it

pifou25 added a commit to pifou25/wideq that referenced this issue Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants