-1

I am a beginner. I can't get a json file to load as a string/dict in Python 3.

import json
data = open("Toys_and_Games_5.json", "r")
file = data.read().decode('utf-8')
reviews = json.loads(file)

Error:

AttributeError: 'str' object has no attribute 'decode'

6
  • 1
    You're probably using .decode without any need for it. Try file = data.read() Commented Dec 15, 2019 at 22:04
  • On stackoverflow.com/questions/46408051/… it works. What's in your json file? Commented Dec 15, 2019 at 22:05
  • 3
    You open the file in text mode, meaning that you already read strings not bytes (so no need to decode them). Commented Dec 15, 2019 at 22:06
  • when I remove the decode, I get the following error... JSONDecodeError: Extra data: line 2 column 1 (char 321) Commented Dec 15, 2019 at 22:10
  • 1
    Check the sintax of your json or parse it here Commented Dec 15, 2019 at 22:14

1 Answer 1

0

This is all you need to do:

import json
data = json.load(open("Toys_and_Games_5.json"))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.