PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 14, 2022

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

 December 14, 2022     javascript, json, python, runtime-error, syntax     No comments   

Issue

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.


Solution

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


Answered By - chepner
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing