简体   繁体   中英

How can I solve this syntax error in my JSON object?

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.

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"
                ]
            }
        }
    ]
}

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"
                    ]
                }
        }
    ]
}

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"
        ]
      }
    }
  ]
}

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"
        ]
      }
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM