Home > other >  json.decoder error: expecting name in double quotes
json.decoder error: expecting name in double quotes

Time:05-28

I'm trying to do an entry code a long project and I hit an error with my .json file trying to load my .py file. The code I have is the exact same and the only difference is that he's using pycharm and I'm using sublime. It says that my json file has no build system, but I have automatic selected. Not sure if that is the potential problem or not.

Error in question:

    json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes

Code in question:

    intents = json.loads(open('intents.json').read())

Json file in question:

{"intents": [
        {"tag": "greeting",
         "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up", "Howdy", "Greetings"],
         "responses": ["Hello!", "Good to see you again!", "Hi there, how can I help?", "What can I do for you?"],
        },
        {"tag": "goodbye",
         "patterns": ["cya", "See you later", "Goodbye", "I am Leaving", "Have a Good day", "I'm out", "Peace", "bye", "cao", "see ya"],
         "responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!", "Have a nice day"],
        },
        {"tag": "age",
         "patterns": ["how old", "how old is Nicolas", "what is your age", "how old are you", "age?"],
         "responses": ["I am 24 years old!", "24 years young!"],
        },
        {"tag": "name",
         "patterns": ["what is your name", "what should I call you", "whats your name?", "who are you", "Can you tell me your name"],
         "responses": ["You can call me Nic.", "I'm Nic!", "I'm Nicolas, or you can just call me Nic."],
        },
        {"tag": "shop",
         "patterns": ["Id like to buy something", "whats on the menu", "what do you serve here?" "what do you reccommend?", "could i get something to eat", "I'd like to place an order"],
         "responses": ["Hello! We sell Subs for $7!", "Subs are on the menu!"],
        },
        {"tag": "hours",
         "patterns": ["when are you guys open?", "what are your hours?", "hours of operation?", "What's your hours like?"],
         "responses": ["We are open 7am-7pm Monday-Friday!"],
        }
   ]

CodePudding user response:

You were missing commas/semicolons in your JSON file and had trailing commas. Reformat it so it looks like this:

{
    "intents": [
        {
            "tag": "greeting",
            "patterns": [
                "Hi",
                "How are you",
                "Is anyone there?",
                "Hello",
                "Good day",
                "Whats up",
                "Howdy",
                "Greetings"
            ],
            "responses": [
                "Hello!",
                "Good to see you again!",
                "Hi there, how can I help?",
                "What can I do for you?"
            ]
        },
        {
            "tag": "goodbye",
            "patterns": [
                "cya",
                "See you later",
                "Goodbye",
                "I am Leaving",
                "Have a Good day",
                "I'm out",
                "Peace",
                "bye",
                "cao",
                "see ya"
            ],
            "responses": [
                "Sad to see you go :(",
                "Talk to you later",
                "Goodbye!",
                "Have a nice day"
            ]
        },
        {
            "tag": "age",
            "patterns": [
                "how old",
                "how old is Nicolas",
                "what is your age",
                "how old are you",
                "age?"
            ],
            "responses": [
                "I am 24 years old!",
                "24 years young!"
            ]
        },
        {
            "tag": "name",
            "patterns": [
                "what is your name",
                "what should I call you",
                "whats your name?",
                "who are you",
                "Can you tell me your name"
            ],
            "responses": [
                "You can call me Nic.",
                "I'm Nic!",
                "I'm Nicolas, or you can just call me Nic."
            ]
        },
        {
            "tag": "shop",
            "patterns": [
                "Id like to buy something",
                "whats on the menu",
                "what do you serve here?",
                "what do you reccommend?",
                "could i get something to eat",
                "I'd like to place an order"
            ],
            "responses": [
                "Hello! We sell Subs for $7!",
                "Subs are on the menu!"
            ]
        },
        {
            "tag": "hours",
            "patterns": [
                "when are you guys open?",
                "what are your hours?",
                "hours of operation?",
                "What's your hours like?"
            ],
            "responses": [
                "We are open 7am-7pm Monday-Friday!"
            ]
        }
    ]
}

CodePudding user response:

Strictly speaking, your JSON is not valid, because of the trailing commas in each object in the intents array e.g.:


{"tag": "greeting",
 "patterns": ["Hi", "How are you", "Is anyone there?", "Hello", "Good day", "Whats up", "Howdy", "Greetings"],
 "responses": ["Hello!", "Good to see you again!", 
               "Hi there, how can I help?", "What can I do for you?"
              ],
}
               ^^^^ illegal trailing comma here 

If you remove these commas, the JSON will load fine using json.loads

Note that jsonlint.com is a good place to validate your JSON.

  • Related