1

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

4
  • Can you print out the value of the s3 key contents? The error usually indicates the content is not valid json format or the file is blank.
    – Micky Loo
    Commented Nov 17, 2019 at 17:55
  • @Micky Loo : i have put the contents of the object in the question above. Is there a way to fix the json module ? or do we have to clean install ?
    – marie20
    Commented Nov 17, 2019 at 18:32
  • The traceback shows the error for the line jsonStr = json.loads(jsonVal), but your code only has the line jsonStr = json.loads(jsonVal['Body'].read()). Which code are you actually running? Commented Nov 17, 2019 at 18:45
  • i figured I was using the wrong json object file. Thanks
    – marie20
    Commented Nov 17, 2019 at 18:48

1 Answer 1

0

When opening your file for reading it, try encoding it like:

jsonFile=open("Your_file.json","r",encoding="utf-8")

then parsing it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.