Python Forum

Full Version: JSON file Loading issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am getting below error, tried multiple ways, which are available online.
but still i am getting the error. could you please let me what is reason for error.
and how can i fix it.

Code i am running:
import nltk
from nltk.stem.lancaster import LancasterStemmer
import numpy as np
import tflearn
import tensorflow as tf
import random
import json
import string
import unicodedata
import sys

# a table structure to hold the different punctuation used
tbl = dict.fromkeys(i for i in range(sys.maxunicode)
                    if unicodedata.category(chr(i)).startswith('P'))


# method to remove punctuations from sentences.
def remove_punctuation(text):
    return text.translate(tbl)

# initialize the stemmer
stemmer = LancasterStemmer()
# variable to hold the Json data read from the file
data = None

# read the json file and load the training data
with open('D:\\News wype\\Sample_Data.json', 'r', encoding='utf8', errors='ignore') as json_data:
    data = json.load(json_data)
    print(data)
error I am getting:

Error:
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode obj, end = self.scan_once(s, idx) JSONDecodeError: Invalid \escape
make sure your json file is valid. e.g. https://jsonlint.com/
Is there a file path in your json file?

Sometimes with Windows it's much less troublesome to just use fwd slashes in your absolute paths... like in Linux.
Hello,

try to change the put like this

D://News wype//Sample_Data.json when you load the data from path in python you need to change the \ with / .
Thanks
@0LI5A3A, it's an old thread but because you necro-post - there is nothing wrong with using double backslash in path - this will escape the backslash in windows path. Basically there are three ways to do it:
  • using raw-string
  • using double backslash, to escape the backaslash and possible escape sequence.
  • using forward slash, like you suggest.

In addition - the traceback show that the file is found (i.e. no problem with path) and there is problem with the content of the file.