Reputation: 35
r = requests.get(http_get_url, headers=headers)
r = r.text
r = r.replace("true", "True")
z = json.loads(r)
however, instead of loading the json (or python dict), I get:
Traceback (most recent call last):
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 21, in <module>
get_errors(id)
File "/home/noir/PycharmProjects/Work_Projects/get_errors.py", line 17, in get_errors
z = json.loads(r)
File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 26 (char 28)
The result of print(type(r))
is <class 'str'>
and the output of print(r)
after converting 'true' to 'True' is:
{
"HasItemsWithCount": True,
"Collection": [
{
"GroupId" : "14",
"Time" : "5/16/18, 5:02 PM",
"File" : "[ESCAPE[]]",
"Message" : "[ESCAPE[Client was restarted during backup, session may be incomplete.]]",
"Count" : "3"
},
]
}
So I fail to understand why the correctly formatted string of r fails to import into json.
Also, if I take this output and write it directly into python via copy/paste, the type of the variable is dict, showing me that the text is formatted correctly for a dict in Python. So I'm not sure why json.loads fails.
Upvotes: 1
Views: 189
Reputation: 49774
Your replace is backwards:
Instead of:
r = r.replace("true", "True")
Try:
r = r.replace("True", "true")
Upvotes: 1