skip to Main Content
{
    "names": [
        {
            "firstname": "Jim",
            "age": 32

        },
        {
            "firstname": "Test",
            "age": 32
        }
        {
            "firstname": "Bob",
            "age": "28"
        }
    ]
}

Above is my current JSON script. I’m trying to update my current JSON script with this code below:

import json

def write_json(data, filename="05_new_json_script.json"):
    with open (filename, "w") as f:
        json.dump(data, f, indent=4)

with open ("05_new_json_script.json") as json_file:
    data = json.load(json_file)
    temp = data["names"]
    y = {"firstname": "Joe", "age": 40}
    temp.append(y)

write_json(data)

However, I have received the error:

Exception has occurred: JSONDecodeError
Expecting ',' delimiter: line 12 column 9 (char 176)
  File "C:UsersUserOneDrive - Innergia Labs Sdn BhdPython Practicesample.py", line 8, in <module>
    data = json.load(json_file)
           ^^^^^^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 12 column 9 (char 176)

The intended result for the code is to have added script of:

        {
            "firstname": "Joe",
            "age": "40"
        }

2

Answers


  1. The problem is that you’re missing a ",".

    {
                "firstname": "Test",
                "age": 32
    }
    

    here you need , at the end of the object like:

    {
                "firstname": "Test",
                "age": 32
    },
    
    Login or Signup to reply.
  2. The Json is broken at row number 10. adding a comma will be solving the problem for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search