Home > Software engineering >  How can I solve this syntax error in my JSON object?
How can I solve this syntax error in my JSON object?

Time:08-05

First and foremost, I'd like to address that JSON is fairly new to me so expect my code to look faulty. I have a JSON object that defines two clubs: "Oak Club" and "Bravo Club" with its members. Note, that this code was written in Python.

Here is the JSON object:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
            }
    ],
    [
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
            }
        ]
    }

This code raises the syntax error: ':' expected after dictionary key.

VSCode highlights the "clubs" key-value pair and states: End of file expected.

After inspecting my code, I can't seem to figure out where I went wrong since all my keys have colons separating keys from their values.

CodePudding user response:

You are trying to map clubs to two separate arrays (each containing a single object), rather than a single array with two objects.

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
        },
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
        }
    ]
}

CodePudding user response:

Rather than including the two club objects inside of the clubs array, you only adding the Oak Club object to it and then defining another array to the JSON object that is anonymous. Here's a better way to structure that code:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                "members": [
                    "Charles",
                    "Nora",
                    "Tom",
                    "Paul"
                ]
            }
        },
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                ]
            }
        }
    ]
}

CodePudding user response:

Line 13 you're closing the array instead of the object.

json_clubs = {
  "clubs": [
    {
      "Oak Club": {
        "members": [
          "Charles",
          "Nora",
          "Tom",
          "Paul"
        ]
      },
      "Bravo Club": {
        "members": [
          "Nathaniel",
          "Roselyn",
          "Ash"
        ]
      }
    }
  ]
}

CodePudding user response:

In the current format, you are missing a key and colon for the second item. WIthout changing it, you would fix by entering its key here:

json_clubs = {
    "clubs": [
        {
            "Oak Club": {
                    "members": [
                        "Charles",
                        "Nora",
                        "Tom",
                        "Paul"
                        ]
                    }
            }
    ],
    "KEY NAME GOES HERE" : [
        {
            "Bravo Club": {
                "members": [
                    "Nathaniel",
                    "Roselyn",
                    "Ash"
                    ]
                }
            }
        ]
    }

However, based on the item names, it appears that maybe something like this would be the more appropriate solution.

json_clubs = {
  "clubs": [
    {
      "Oak Club": {
        "members": [
          "Charles",
          "Nora",
          "Tom",
          "Paul"
        ]
      },
      "Bravo Club": {
        "members": [
          "Nathaniel",
          "Roselyn",
          "Ash"
        ]
      }
    }
  ]
}
  • Related