Salamek / huawei-lte-api

API For huawei LAN/WAN LTE Modems
GNU Lesser General Public License v3.0
376 stars 92 forks source link

Invalid json from at least get_sms_list #101

Closed asarubbo closed 3 years ago

asarubbo commented 3 years ago
#!/usr/bin/python3.8

from huawei_lte_api.Client import Client
from huawei_lte_api.AuthorizedConnection import AuthorizedConnection
import sys

connection = AuthorizedConnection('http://admin:pass@192.168.8.1/')
client = Client(connection)

print(client.sms.get_sms_list())

client.user.logout()

I got:

{'Count': '2', 'Messages': {'Message': [{'Smstat': '0', 'Index': '40001', 'Phone': 'ho.', 'Content': 'Ti informiamo che la tua offerta si è rinnovata. Il prossimo rinnovo è fra un mese a partire da oggi.', 'Date': '2021-03-26 08:09:02', 'Sca': None, 'SaveType': '0', 'Priority': '0', 'SmsType': '2'}, {'Smstat': '0', 'Index': '40000', 'Phone': 'ho.', 'Content': 'La tua offerta si rinnoverà fra 2 giorni. Assicurati di avere credito disponibile sull’app o chiamando gratuitamente il 42121.', 'Date': '2021-03-24 08:07:38', 'Sca': None, 'SaveType': '0', 'Priority': '0', 'SmsType': '2'}]}}

Json validator says that it is invalid, infact I can't parse it with jq.

1) 'Sca': None should be 'Sca': 'None'

( missing single quote)

2) All strings should have double quote.

I can get a valid json with the following ( just for example ):

./huawei_sms.py | sed "s:None:'None':g" | sed "s:':\":g"
Salamek commented 3 years ago

@asarubbo plot twist: that is NOT JSON!

It is print of Python dict, you are basically doing this:

python_dict  = {
    'a': None,
    'b': 'lol'
}

print(python_dict)

{'a': None, 'b': 'lol'}

And that is expected... if you want JSON on stdout you must do this (convert python dict into JSON string):

import json
python_dict  = {
    'a': None,
    'b': 'lol'
}

print(json.dumps(python_dict))

{"a": null, "b": "lol"}

So in your example code:

#!/usr/bin/python3.8

from huawei_lte_api.Client import Client
from huawei_lte_api.AuthorizedConnection import AuthorizedConnection
import sys
import json

connection = AuthorizedConnection('http://admin:pass@192.168.8.1/')
client = Client(connection)

print(json.dumps(client.sms.get_sms_list()))

client.user.logout()
Salamek commented 3 years ago

Well i think this is solved... closing

asarubbo commented 3 years ago

Hi, I'm not a python developer so I didn't know python dict :)

Thanks, it works like a charm!