添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Errors solved , Python

How to fix raise jsondecodeerror(“expecting value”, s, err.value) from none in python

Md Niaz Rahman Khan
  • No Comments
  • In this article, you are going to learn about how to fix raise jsondecodeerror(“expecting value”, s, err.value) from none in python.

    JSON refers to the JavaScript Object Notation which is very lightweight and easy to parse data. In python, while working with APIs you may need to use the JSON for parsing data. But, if this JSON file is malformed then you may have faced some unwanted situation.

    Recently, while working with JSON in my python program, I have faced similar kinds of issues and in this article, I will explain how to fix this issue so that you may focus on the learning and saves your time. Before doing so, let’s see our JSON file first. Here, we have stored dummy employee JSON data in our employee.json file.

    "id": , "name": "Alex", "email": " [email protected] ", "password": "test123", "Position" : "Junior Web Devs", "salary" : 500

    Let’s load this JSON data in python using the file system and see what happens. To do so, see the below code example:

    import json
    path = "C:/Users/LENOVO/Desktop/python/employee.json"
    with open(path, 'r') as data:
        employee_data = json.loads(data.read())
        print(employee_data)

    Here, we have imported the JSON first and then set the full path for our JSON file, and later on, simply print the JSON data. Let’s run our program by giving the python main.py command in our terminal and see what happens:

    You can see that the moment we have run our program, we are getting that error. In the next section, we will see how we can fix this error.

    The Solution

    The solution to this problem is very easy and simple. If you notice carefully, you may be able to see that we have missed a value at the very beginning of our JSON file. We did not set any value "id: and as a result, it gives us this error. To fix this we have to give some value to this id. For example, we have set the "id":1 and run our program again. Now let’s see what happens in the below section:

    import json
    path = "C:/Users/LENOVO/Desktop/python/employee.json"
    with open(path, 'r') as data:
        employee_data = json.loads(data.read())
        print(employee_data)
    # Output: {'id': 1, 'name': 'Alex', 'email': '[email protected]', 'password': 'test123', 'Position': 'Junior Web Devs', 'salary': 500}

    Here, you can see that we are getting the expected output instead of getting that error. To fix this issue is as simple as this. In your case, the reason may be different from mine but it is not a complex reason behind this error. All you need to do is to observe the error message or the JSON file carefully and you may find the solution for it.

    This guide is part of the “ Common Python Errors ” series. It’s focused entirely on providing quick and easy solutions for Python-related problems.