How to Fix json.decoder.JSONDecodeError: Expecting value: line 1 column 1

JSONDecodeError: Expecting value: line 1 column 1 (char 0) error typically occurs when you “parse a string as JSON, but the string is not in a valid JSON format.”

Common reasons

  1. Empty Input: The JSON data you’re trying to decode is an empty string.
  2. Invalid Input: The data you’re trying to decode isn’t JSON-formatted.
  3. Source Issues: If you’re fetching the JSON from a URL or file, there might be an issue with the source. The URL might not return any data, or the file might be empty.
  4. Empty json file: Trying to misread a JSON file or trying to parse the contents of an empty JSON file.

How to Fix

  1. Check the JSON: Ensure your decoding JSON data isn’t empty. If it’s a file, open it and check its contents.
  2. Validate the JSON: Use a JSON validator tool to properly format the JSON data.
  3. Logging: If you’re fetching the JSON dynamically (e.g., from a URL), logging the response content might be a good idea to see what you’re getting.
  4. Ensure that the input is a string because the json.loads() function can only work on strings.

Issue 1: Decoding empty json

    import json
    
    result = json.loads('')
    
    print(result)
    

    Output

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

    Fix 1

    import json
    
    main_str = ''
    
    try:
      result = json.loads(main_str)
    except json.decoder.JSONDecodeError:
      print('The string is empty or does NOT contain valid JSON')

    Output

    The string is empty or does NOT contain valid JSON

    Issue 2: Decoding invalid JSON content

    Python json library requires you to pass valid JSON content when calling the load() or loads() function.

    import json
    
    data = '{"name": Nathan}'
    
    result = json.loads(data)
    
    print(result)
    

    Output

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

    Fix 2

    You can use a try-except block to check if your data is a valid JSON string like this:

    import json
    
    data = '{"name": Nathan}'
    
    try:
      result = json.loads(data)
    except json.decoder.JSONDecodeError:
      print('The string does NOT contain valid JSON')

    Output

    The string does NOT contain valid JSON

    Issue 3: Empty or invalid JSON file

    Another case when this error may happen is when you load an empty .json file.

    import json
    
    with open("data.json", "r") as file:
      data = json.loads(file.read())
      print(data)

    Output

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

    Fix 3

    import json
    
    try:
      with open("data.json", "r") as file:
      data = json.loads(file.read())
      print(data)
    except json.decoder.JSONDecodeError:
      print("File is empty or contain invalid JSON")

    Output

    File is empty or contain invalid JSON

    Issue 4: The server didn’t return a valid JSON

    When you send an HTTP request using the requests library, you can “use the .json()” method from the response object to extract the JSON content:

    import requests
    
    response = requests.get('https://api.github.com')
    
    data = response.json()
    
    print(data)

    Output

    requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1

    Fix 4

    To fix this error, you need to surround the call to response.json() with a try-except block:

    import requests
    
    response = requests.get('https://api.github.com')
    
    try:
      data = response.json()
      print(data)
    except:
      print("Error from server: " + str(response.content))

    When the except block is triggered, the response content will be printed in a string format.

    That’s it!

    Related posts

    Pylint unresolved import error in Python

    IndentationError: expected an indented block

    ValueError: i/o operation on closed file

    Leave a Comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.