0

This is the error I keep getting

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is what the json data looks like:

{
    "TITLE": "Rome: The Punic Wars - The First Punic War - Extra History - #1",
    "CHANNEL_NAME": "Extra Credits",
    "NUMBER_OF_VIEWS": "3,954,083 views",
    "LIKES": "50,431",
    "DISLIKES": "887",
    "NUMBER_OF_SUBSCRIPTIONS": "2.37M",
    "HASH_TAGS": []
}

I've tried to modify the file to point of deleting all but the curly brackets, then even them to, but the error still happens.

This is what my code looks like:

import json

with open('C:/Users/longv/Desktop/data.json') as f:
    print('hello')
    data = json.load(f)

I also searched StackOverflow, but the discussions are so over my head I don't even understand, and it doesn't look like it'd work in my case anyway.

EDIT: This is a load of BS, because I used the online platform repl.it and this code worked fine.

6
  • 1
    Did you try with json.loads()? Commented Jun 1, 2020 at 16:42
  • Have you tried getting rid of each element at a time to rule out an encoding issue with a particular part of the file? Commented Jun 1, 2020 at 16:42
  • your example object works fine for me, without errors Commented Jun 1, 2020 at 16:44
  • everything works also on my end, what python and json version did you used? Commented Jun 1, 2020 at 16:49
  • @bro I'm using python 3.6.0, for json version I have no idea. I only know import son. Let me try to update to the newest python version Commented Jun 1, 2020 at 16:58

2 Answers 2

2

First, you have to put the JSON file and the python code in the same directory. Then try to write the following:

with open('yourjsonfile.json') as f:
    data = json.load(f)
# then,if you want to print out the "TITLE" in your context
print(data['TITLE'])
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a reason why the JSON file have to be in the same directory when I can import a TXT or CSV file from a specified file path? I'm asking because I don't know where my python code is located.
1

Maybe it's the indentation. Indenting the with block like so might fix it:

import json

with open('C:/Users/longv/Desktop/data.json') as f:
    print('hello')
    data = json.load(f)

1 Comment

When I copied and paste the format is exactly what I already had

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.