I have a issues loading a json file of user and password in python to retrieve data from the rest api which uses authentication to extract the json data from the url.
When I have put the user, password and uri in the json file and run the script it gives me this error in the json libary :
Traceback (most recent call last):
File "C:/Python27/Script.py", line 10, in <module>
config = json.load(config_file)
File "C:\Python27\lib\json\__init__.py", line 290, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Invalid \escape: line 7 column 42 (char 270)
This is the code:
import urllib2
import json
#Load the config file
with open('Config.json') as config_file :
config = json.load(config_file)
# Load your username from the config file
user = config['user']
# Load your password from the config file
password = config['password']
enter code here
The json file from which it loads sample is here:
{
"user" : "api",
"password" : "admin1234",
"uri": "https://datafeeds.emailsecurity.com/test",
"resetUri": "https://datafeeds.emailsecurity.com/test/test?reset=2017-07-01T00:00:00Z",
"files" : {
"cookiesFilePath" : "C:\\abc",
"logsFilePath" : "C:\\abc",
}
}
The problem is the double backslash as others have said. You want to escape both of the backslashes, for example:
bad_str = '{"a" : "C:\\ b"}'
good_str = '{"a" : "C:\\\\ b"}'
#JSONDecodeError
print(json.loads(bad_str))
# OK
print(json.loads(good_str))
The data file contains invalid json:
>>> json.loads('{"cookiesFilePath": "C:\\abc"}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
...
ValueError: Invalid \escape: line 1 column 24 (char 23)
The problem is the "c:\\\\abc"
strings. the json decoder interprets the double backslash as an escaped single backslash, making the character after the colon '\\a'
, which is not valid json.
It isn't possible to generate the json in your example using the standard python json encoder:
>>> json.dumps({"cookiesFilePath": "C:\abc"})
'{"cookiesFilePath": "C:\\u0007bc"}'
>>> json.dumps({"cookiesFilePath": "C:\\abc"})
'{"cookiesFilePath": "C:\\\\abc"}'
which suggests that the json in your file has been created using a broken tool, or edited manually.
{
"user" : "api",
"password" : "admin1234",
"uri": "https://datafeeds.emailsecurity.com/test",
"resetUri": "https://datafeeds.emailsecurity.com/test/test?reset=2017-07-01T00:00:00Z",
"files" : {
"cookiesFilePath" : "C:\\abc",
"logsFilePath" : "C:\\abc"
}
}
Try without comma in json file.
With comma:
"logsFilePath" : "C:\\abc",
Without comma at end:
"logsFilePath" : "C:\\abc"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.