0

I got a simple Language model code in python that needs to load an "intents.json" file.

I'm very new to coding so I appreciate any help. I've tried multiple different strategies to try and load the json file. None with success. This is the current code:

file_path = os.path.join('C:/Users/ed/Desktop/intents.json')

with open(file_path, encoding='cp1252') as f:
    intents = json.load(f)

This is the intents.json file. I've ran it through a json validator and it has come back as valid. Here is the code:

{
    "intents": [{
            "tag": "greeting",
            "patterns": ["Hi there", "Hello", "Hey", "Hi"],
            "responses": ["Hello, how can I help you?", "Hi there! How can I assist you today?", "Hi! How may I assist you?", "Hello! How can I help you today?"]
        },
        {
            "tag": "goodbye",
            "patterns": ["Bye", "See you later", "Goodbye", "Have a nice day"],
            "responses": ["Goodbye! Have a nice day", "See you later!", "Hope to see you soon!", "Take care!"]
        },
        {
            "tag": "thanks",
            "patterns": ["Thanks", "Thank you", "That's helpful", "Thank's a lot"],
            "responses": ["You're welcome!", "Anytime!", "Glad I could help!", "No problem!"]
        }
    ]
}

I have a multitude of errors coming back but the most prominent ones are these:

-  raise JSONDecodeError("Expecting value", s, err.value) from None
 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

- File "x:\LM\LMF\ANN2\main.py", line 14, in <module>
    intents = json.load(f)

Again, thanks again for any help.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • 3
    That error usually means that the file is empty. – Barmar May 05 '23 at 19:44
  • The posted json file is valid. The actual json file on your computer must be different. – John Gordon May 05 '23 at 19:45
  • You can check the related post [here](https://stackoverflow.com/questions/16573332/jsondecodeerror-expecting-value-line-1-column-1-char-0). – Vasilis G. May 05 '23 at 19:45
  • Minor point, but why are you using `os.path.join()` on just one string? – John Gordon May 05 '23 at 19:46
  • 2
    If you're using Windows, you'll need to use \\ as the path separator. Alternatively, take a look at using [`pathlib`](https://docs.python.org/3/library/pathlib.html) or [`os.path.join`](https://docs.python.org/3/library/os.path.html#os.path.join). – rayryeng May 05 '23 at 19:46
  • 2
    @rayryeng Forward slashes work just fine in Python code on windows. (They don't work at the command prompt, but in code they do.) – John Gordon May 05 '23 at 19:47
  • @Winston `json.load()` threw an exception, so the `intents` variable was not created. – John Gordon May 05 '23 at 19:49

0 Answers0