0

I have a file Json_data.txt which is stored locally. The content of the file is a JSON object as given below:

{
    "directed": "true",
    "multigraph": "true",
    "graph": {"directed": "true"},
    "nodes": [
        {"id": "Brave Browser", "shape": "circle"},
        {"id": "Windows Explorer", "shape": "circle"},
        {"id": "Spotify", "shape": "circle"},
        {"id": "Windows Shell Experience Host", "shape": "circle"},
        {"id": "Timeular", "shape": "circle"}
    ],
    "links": [
        {"source": "Brave Browser", "target": "Brave Browser", "time": 4, "weight": 4},
        {"source": "Brave Browser", "target": "Windows Explorer", "time": 2, "weight": 2},
        {"source": "Windows Explorer", "target": "Spotify", "time": 1, "weight": 1},
        {"source": "Spotify", "target": "Spotify", "time": 1, "weight": 1},
        {"source": "Spotify", "target": "Windows Explorer", "time": 1, "weight": 1},
        {"source": "Windows Explorer", "target": "Windows Shell Experience Host", "time": 1, "weight": 1},
        {"source": "Windows Shell Experience Host", "target": "Brave Browser", "time": 1, "weight": 1},
        {"source": "Windows Explorer", "target": "Timeular", "time": 1, "weight": 1},
        {"source": "Timeular", "target": "Timeular", "time": 1, "weight": 1},
        {"source": "Timeular", "target": "Brave Browser", "time": 1, "weight": 1}
    ]
}

I am trying to read the above JSON in the HTML page for further processing. I tried using src in the script tag to link the file and tried to parse it as "script type="text/javascript" src="file:///C:/file_path/Json_data.txt". I am trying to get the content into test:

fetch('file:///C:/file_path/Json_data.txt')
    .then(response => response.json())
    .then(test => JSON.parse(json));

The above code is not working. Could anyone please tell me what am I doing wrong here?

Swap2019
  • 93
  • 1
  • 6
  • 2
    Reading files from the disk from a web browser is generally not allowed due to sandboxing & security rules. Better to host the file on your web server as a resource. – PaulProgrammer Jun 08 '20 at 20:47
  • Open the browsers developer tools, look at the console, read the error messages. – Quentin Jun 08 '20 at 20:53
  • It says 'Not allowed to load local resource' – Swap2019 Jun 08 '20 at 20:57
  • `json` is not the same variable as `test`, for one thing. Also, `Json_data.js` is not the same as `Json_data.txt`, which is what your question calls the file in the first sentence. Finally, you can't read files from the `file:` scheme over `fetch` without changing the security of your browser. – Heretic Monkey Jun 08 '20 at 20:57
  • I thought I updated it to .txt instead of .js. Even then it is not working. Is there any other way to do it? I am very new to html and Javascript. So I might be doing wrong completely. – Swap2019 Jun 08 '20 at 21:00
  • "It says 'Not allowed to load local resource'" — Yes, hence the duplicate question. – Quentin Jun 08 '20 at 21:02

0 Answers0