Python Forum

Full Version: json loads throwing error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I have python code written using netmiko library which does SSH to a switch and run the command "show env power | json". I need to convert this out put to json format so that I can track individual keys to check the power supply status.
arista_commands = ["show env power | json"]

print("Device is Arista")
        for command in arista_commands:
                print("No of working PSU's on: " + ssh1.send_command(command))
                psu_data = json.loads(ssh1.send_command(command))
Error:
Traceback (most recent call last): File "/home/smulgund/device_psu_test.py", line 33, in <module> psu_data = json.loads(ssh1.send_command(command)) File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I am getting error message while convertign the string data to JSON using json.loads().
Error:
Traceback (most recent call last): File "/home/smulgund/device_psu_test.py", line 33, in <module> psu_data = json.loads(ssh1.send_command(command)) File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads return _default_decoder.decode(s) File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/local/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What is returned by ssh1.send_command("show env power | json")? According to the error message it is a json string.
the output returned from ssh1.send_command("show env power | json") is class str
(Jan-22-2024, 09:55 AM)mpsameer Wrote: [ -> ]the output returned from ssh1.send_command("show env power | json") is class str
As mention you most make a variable and post what the result is.
It most return a valid json string for json.load() to work.
arista_commands = ["show env power | json"]
for command in arista_commands:
    raw_output = ssh1.send_command(command)
    print(raw_output) # Most return a valid json string 
To give a example on how a valid json string looks.
>>> import json
>>> raw_output = '{"name":"John", "age":30, "city":"New York"}'
>>> data = json.loads(raw_output)
>>> data
{'age': 30, 'city': 'New York', 'name': 'John'}
>>> data['city']
'New York'
Quote:the output returned from ssh1.send_command("show env power | json") is class str
All json format strings are instances of str, but not all str object are json format strings. Your string is not a json format string. Get the string and print the string. It will look nothing like a json string.
print("No of working PSU's on: " + ssh1.send_command(command))
                psu_data = ssh1.send_command(command)
                print(type(psu_data))
This returns type as
Output:
<class 'str'>
What is the value of psu_data?