I have recently installed latest Python 3.8
, boto3
and the latest aws-cli
package on my PC running Windows 10
.
I have configured aws cli and boto3 correctly as AWS commands work fine. However, when I try to read a JSON file, the Python interpreter throws an error :
jsonStr = json.loads(jsonVal)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\HomeUser\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\HomeUser\AppData\Local\Programs\Python\Python38-32\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\HomeUser\AppData\Local\Programs\Python\Python38-32\lib\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)
The code I am trying to run is given below:
import os
import sys
import json
import string
import boto3
s3 = boto3.client('s3')
jsonVal = s3.get_object(Bucket=my_bucket, Key=my_key_path)
jsonStr = json.loads(jsonVal['Body'].read())
All statements prior to json.loads()
work fine giving expected output, but fails in the json.loads()
step. The object I am trying to retrieve is a very small document from S3
.
Earlier, the json package used to work fine but have started throwing errors off late. It even fails to parse (json.loads()
) other json files which it had successfully parsed in the past.
I think my json library has gone corrupt. Can anyone please identify the issue and let me know how to fix this ? Please let me know if additional information is required.
The contents of the key file is :
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
Thanks
jsonStr = json.loads(jsonVal)
, but your code only has the linejsonStr = json.loads(jsonVal['Body'].read())
. Which code are you actually running?