09-04-2024 04:13 PM
Hello,
I wanted to filter from following json load retried from WebEx Room list and print only the rooms.
The current script returns the following error:
json.decoder.JSONDecodeError
Traceback (most recent call last):
File "jsonWebExGetRoomFromList.py", line 23, in <module>
items = json.loads(json_data)
File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1)
Script:
09-05-2024 12:02 AM
Hi, the string in your json_data is not valid JSON. Try to delete the opening (' and closing '):
json_data = """
{"items":[
{"id":"room1foobar"},
{"id":"room2foobar"},
{"id":"room3foobar"}
]}
"""
09-06-2024 05:58 AM
Hi Marcel,
Thank you for your suggestion, but I am still receiving the same error message:
json.decoder.JSONDecodeError: Invalid control character at: line 2 column 137 (char 137)
PS D:\Network_ComputerScience\CISCO\Certifications\CiscoDevNet\Cisco Certified DevNet Associate DEVASC 200-901\Python Scripts>
Removed ('...')
09-06-2024 07:00 AM
yeah, beacause the string in your json_data variable is still not valid JSON. Use a linter (for example online https://jsonlint.com/), to check if the string is valid JSON.
09-06-2024 10:27 AM
Confirmed - not json format.
Is there any other way to extract the value of key 'title'" of this string?
09-09-2024 05:58 AM
If you want to list the titles of all your webex-rooms, you can use the following script.
import requests
import os
webex_access_token = os.getenv("WEBEX_TOKEN")
headers = {
"Authorization": f"Bearer {webex_access_token}"
}
r = requests.get("https://webexapis.com/v1/rooms", headers=headers)
if r.ok:
json_data = r.json()
room_titles = [room.get("title") for room in json_data.get("items")]
print("WEBEX-ROOMS:")
for title in sorted(room_titles):
print(title)
else:
print(r.status_code)
print(r.text)
before running it you need to store your access token in a env var named WEBEX_TOKEN:
export WEBEX_TOKEN="<your access token>"
HTH
Marcel
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide