Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
json loads throwing error
#1
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)
Yoriz write Jan-21-2024, 09:20 AM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
I am getting error message while convertign the string data to JSON using json.loads().
Reply
#3
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)
Reply
#4
What is returned by ssh1.send_command("show env power | json")? According to the error message it is a json string.
Reply
#5
the output returned from ssh1.send_command("show env power | json") is class str
Reply
#6
(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'
Reply
#7
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.
Reply
#8
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'>
Reply
#9
What is the value of psu_data?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  json decoding error deneme2 10 3,680 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  python requests library .JSON() error mHosseinDS86 6 3,453 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  Read nested data from JSON - Getting an error marlonbown 5 1,383 Nov-23-2022, 03:51 PM
Last Post: snippsat
  pywin32 Illustrator Throwing Exception Error matthewsjc1 7 5,606 Aug-27-2021, 02:43 AM
Last Post: Larz60+
  JSON Decode error when using API to create dataframe Rubstiano7 4 2,962 Jan-11-2021, 07:52 PM
Last Post: buran
  Empty response to request causing .json() to error t4keheart 1 10,088 Jun-26-2020, 08:35 PM
Last Post: bowlofred
  empty json file error mcmxl22 1 10,161 Jun-17-2020, 10:20 AM
Last Post: buran
  PyAudio throwing Input overflowed anthares 3 4,801 Jun-14-2020, 03:37 PM
Last Post: anthares
  json.dumps output error in python3 prayuktibid 2 2,665 Jan-21-2020, 06:41 AM
Last Post: prayuktibid
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 2,930 Sep-22-2019, 05:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020