0

I have the following text in the file "lineValuesJSON.json".

{"4": {"0": {"0": {"0": {"0": [0, 0, 0, 0]}}}}}

I load this file in HTML via

<script type="text/javascript" src="../js/moveRating/lineValuesJSON.json"></script>

and parse it then in Javascript with

const lineValues = JSON.parse(lineValuesJSON);

I get the following error:

Uncaught SyntaxError: Unexpected token ':'

I have checked the json code on https://jsonformatter.curiousconcept.com/, it says that it is valid. Does anyone know why I get this error?

sw0816
  • 37
  • 8

1 Answers1

0

JSON isn't JavaScript. text/javascript is the wrong MIME type for JSON. JSON is not a script. <script> elements don't create variables named after the file name of the script loaded. You can't use a <script> element to load JSON.

If you want to load JSON from a URL, request it using the fetch or XMLHttpRequest APIs.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335